Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement NSString.boolValue #51

Merged
merged 1 commit into from
Dec 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,16 @@ extension NSString {

public var boolValue: Bool {
get {
NSUnimplemented()
let scanner = NSScanner(string: _swiftObject)
// skip initial whitespace if present
scanner.scanCharactersFromSet(NSCharacterSet.whitespaceCharacterSet())
// scan a single optional '+' or '-' character, followed by zeroes
if scanner.scanString(string: "+") == nil {
scanner.scanString(string: "-")
}
// scan any following zeroes
scanner.scanCharactersFromSet(NSCharacterSet(charactersInString: "0"))
return scanner.scanCharactersFromSet(NSCharacterSet(charactersInString: "tTyY123456789")) != nil
}
}

Expand Down
12 changes: 12 additions & 0 deletions TestFoundation/TestNSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TestNSString : XCTestCase {

var allTests : [(String, () -> ())] {
return [
("test_boolValue", test_boolValue ),
("test_BridgeConstruction", test_BridgeConstruction ),
("test_isEqualToStringWithSwiftString", test_isEqualToStringWithSwiftString ),
("test_isEqualToObjectWithNSString", test_isEqualToObjectWithNSString ),
Expand All @@ -37,6 +38,17 @@ class TestNSString : XCTestCase {
("test_rangeOfCharacterFromSet", test_rangeOfCharacterFromSet ),
]
}

func test_boolValue() {
let trueStrings: [NSString] = ["t", "true", "TRUE", "tRuE", "yes", "YES", "1", "+000009"]
for string in trueStrings {
XCTAssert(string.boolValue)
}
let falseStrings: [NSString] = ["false", "FALSE", "fAlSe", "no", "NO", "0", "<true>", "_true", "-00000"]
for string in falseStrings {
XCTAssertFalse(string.boolValue)
}
}

func test_BridgeConstruction() {
let literalConversion: NSString = "literal"
Expand Down