{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Swift - Boolean Types
Summary
This article discusses the boolean types in Swift and contrasts with how these types are used in C and Objective-C.
 
Table of Contents

Boolean Literals

No Int or String Values

Operators on Bool types

 
Article Series
Previous Article:
Test Your Knowledge: Swift - Floating Point Types
This article is part of the Series:
Swift - Introduction and Basics
Next Article:
Test Your Knowledge: Swift - Boolean Types

Boolean Literals

In Swift, there are two boolean literals - true and false. If you assign any of these values to a variable or constant, Swift will infer that to be a boolean. Name of that type is Bool.

var gameIsOver = false
// Type of gameIsOver is Bool

// So, this is same as
var gameIsOver: Bool = false

Note that name of the type is Bool, not bool or boolean or Boolean. In Swift UpperCamelCase is used naming Types and Protocols. For all others (methods/functions, properties (variables, constants), enum cases, etc.) naming is done using the lowerCamelCase convention.

Bool is a Type - a struct in Swift. Bool struct has a default initializer, which initializes the variable/constant to the false boolean literal.

var isTest = false
print(isTest)
// Prints: false

// That is same as the following
var isTest = Bool()
print(isTest)
// Prints: false

// Following will give error
var isTest: Bool
print(isTest)
// Here a non-optional isTest variable has been used
// before being initialized

No Int or String Values

In Swift, constructs like if and while work only with Bool values. This is different from C/Objective-C, where boolean values are actually checked for value of 0 or non-0.

bool isTest = 1;
isTest = 100;
isTest = -100;
isTest = true;
isTest = YES;

// In C/Obj-C, all the above will test true and print True in
// the if statement below.
if (isTest) {
  printf(“True\n”);
} else {
  printf(“False\n”);
}

// So would the following testStr would test true
char *testStr = “Test”;

// The following will be considered false in C/Obj-C
isTest = 0;
isTest = false;
isTest = NO;

char *testStr = nil;
char *testStr = 0;

So, in C/Obj-C, true and YES are #defined to 1, while false and NO are #defined to 0. Since a nil pointer is pointing to 0 address, the objects with nil value will test as false in the if and while control constructs.

In Swift, if and while check for true boolean literals - not 0 and non-0. So, optional nil objects won’t test false — won’t even compile.

var isTest = 1

// Won’t compile
if (isTest) {
  print(“True”)
}

The error above says Int does not conform to the BooleanType protocol. The Swift struct Bool conforms to the BooleanType protocol. Similarly, a nil value (coming from an optional) cannot be checked directly for true/false using if/while and other control constructs.

var testStr: String?
// testStr above is a String optional; hence it will have a nil value
// until a proper string is assigned to it.

// Following will not compile. The if statement only tests for
// boolean values - true/false
if(testStr) {
  print(testStr)
} else {
  print(“No String”)
}

// However, you can check explicitly for nil value
// Following compiles and works
if (testStr != nil) {
  print(testStr)
} else {
  print(“No String”)
}

So, bottom line is that in Swift, Bool type represents true boolean literals and not integers or strings or addresses.

Operators on Bool types

If Bool variable/constant is prefixed with ! (exclamation point), the value will be reversed, i.e. true becomes false and false becomes true.

var finished = false
if (!finished) {
  print(“Completed”)
}

The ! should be at the beginning, if it’s at the end — finished! - then it would try to force unwrap an optional.

And the operators == and != can be used to check the equality between two Bool types.

var test1 = true
var test2 = false

if (test1 == test2) {
  print(“Same State”)
}

if (test1 != test2) {
  print(“Different State”)
}

And then are several operators between various types that return Bool values. For example, Int types have several comparison operators that return Bool.

var x = 5
var y = 10

var z = (x < y) // true
z = (x > y) // false

z = (x <= y) // true
z = (x >= y) // false

z = (x != y) // true
z = (x == y) // false

Take a Quick Quiz on this Article

1. Which of the following can be assigned to a Bool property in Swift?



: YES in Obj-C
Question 1 of 5
Article Series
Previous Article:
Test Your Knowledge: Swift - Floating Point Types
This article is part of the Series:
Swift - Introduction and Basics
Next Article:
Test Your Knowledge: Swift - Boolean Types
Bookmark and Share This

More Articles With Similar Tags
icon-swift-test.jpg
Tags: bool, types, swift
Test on Boolean Types in Swift.
icon-swift-series.jpg
This series of articles goes through the basics of Swift. It introduces Playgrounds and REPL; Variables and Constants; Int, Float, Bool; Tuples, Type Aliases, Type Inference, Type Safety, and more.
icon-swift-article.jpg
Tags: int, types, swift
This article goes through the usage of various integer types in Swift (including Int and UInt).
icon-swift-test.jpg
Tags: int, types, swift
Test on Integer types in Swift.
icon-swift-article.jpg
This article talks about decimal numbers in Swift, including Float, Double, and CGFloat. Also talks about Binary, Octal, and Hexadecimal representations.
About  Contact  Privacy Policy  Site Map