Convert String to Bool in Swift - via API or most Swift-like approach
Solution 1:
There is not built in way AFAIK. Similar method to standard toInt()
could be:
extension String {
var bool: Bool? {
switch self.lowercased() {
case "true", "t", "yes", "y":
return true
case "false", "f", "no", "n", "":
return false
default:
if let int = Int(string) {
return int != 0
}
return nil
}
}
}
Solution 2:
Typecasting along with a nice String extension and you're up and running
extension String {
var boolValue: Bool {
return (self as NSString).boolValue
}}