Skip to content

Commit

Permalink
implement NSString.boolValue
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed Dec 6, 2015
1 parent 0bfcd82 commit c4485db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,12 @@ extension NSString {

public var boolValue: Bool {
get {
NSUnimplemented()
for prefix in ["t", "y", "T", "Y", "1", "2", "3", "4", "5", "6", "7", "8", "9"] {
if _swiftObject.hasPrefix(prefix) {
return true
}
}
return false
}
}

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_FromASCIIData", test_FromASCIIData ),
Expand All @@ -34,6 +35,17 @@ class TestNSString : XCTestCase {
("test_FromMalformedNullTerminatedCStringInUTF8", test_FromMalformedNullTerminatedCStringInUTF8 ),
]
}

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

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

0 comments on commit c4485db

Please sign in to comment.