Skip to content

Instantly share code, notes, and snippets.

@dagronf
Created January 7, 2020 00:35
Show Gist options
  • Save dagronf/2849057e85d0a856ec4f1f4af6a7dcfa to your computer and use it in GitHub Desktop.
Save dagronf/2849057e85d0a856ec4f1f4af6a7dcfa to your computer and use it in GitHub Desktop.
Swift String extension to use NSString's 'bool' interpretation rules.
import Foundation
extension String {
/// Retrieve the contents of the string as a Bool value.
///
/// This property is true on encountering one of "Y", "y", "T", "t", or a digit 1-9—the method ignores any trailing characters. This property is false if the receiver doesn’t begin with a valid decimal text representation of a number.
///
/// The property assumes a decimal representation and skips whitespace at the beginning of the string. It also skips initial whitespace characters, or optional -/+ sign followed by zeroes.
///
/// See: [NSString boolValue](https://developer.apple.com/documentation/foundation/nsstring/1409420-boolvalue)
var boolValue: Bool {
return (self as NSString).boolValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment