Created
July 5, 2016 08:34
-
-
Save startupcode/c3f6a9494cb0b8c1b0307bad39224ddc to your computer and use it in GitHub Desktop.
Swift "String" Extension to convert string values in Bool. You can add any value in "Case" for true and false and it will convert it to Bool.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func convertToBool() -> Bool? { | |
switch self { | |
case "true", "1", "yes": | |
return true | |
case "false", "0", "no": | |
return false | |
default: | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this. I was having problem converting such string values from database to Bool in Swift. It helped me a lot.