Skip to content

Instantly share code, notes, and snippets.

@startupcode
Created July 5, 2016 08:34
Show Gist options
  • Save startupcode/c3f6a9494cb0b8c1b0307bad39224ddc to your computer and use it in GitHub Desktop.
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.
extension String {
func convertToBool() -> Bool? {
switch self {
case "true", "1", "yes":
return true
case "false", "0", "no":
return false
default:
return nil
}
}
}
@vikrambiwal
Copy link

vikrambiwal commented Jul 5, 2016

Thanks for this. I was having problem converting such string values from database to Bool in Swift. It helped me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment