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