I was converting from swift 2 to swift 3. I noticed that I cannot convert a boolean value to integer value in swift 3 :\\ .
let p1 = (\"a\" == \"a\") //true
Bool -> Int
extension Bool {
var intValue: Int {
return self ? 1 : 0
}
}
Int -> Bool
extension Int {
var boolValue: Bool {
return self != 0
}
}
Tested in swift 3.2 and swift 4
There is not need to convert it into Int
Try this -
let p1 = ("a" == "a") //true
print(true) //"true\n"
print(p1) //"true\n"
Int(true) //1
print(NSNumber(value: p1))
You could use hashValue property:
let active = true
active.hashValue // returns 1
active = false
active.hashValue // returns 0
let boolAsInt = <#your bool#> ? 1 : 0
Try this,
let p1 = ("a" == "a") //true
print(true) //"true\n"
print(p1) //"true\n"
Int(true) //1
Int(NSNumber(value:p1)) //1
You could use the ternary operator to convert a Bool to Int:
let result = condition ? 1 : 0
result
will be 1 if condition
is true, 0 is condition
is false.