Skip to content

Commit c4485db

Browse files
committed
implement NSString.boolValue
1 parent 0bfcd82 commit c4485db

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Foundation/NSString.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,12 @@ extension NSString {
459459

460460
public var boolValue: Bool {
461461
get {
462-
NSUnimplemented()
462+
for prefix in ["t", "y", "T", "Y", "1", "2", "3", "4", "5", "6", "7", "8", "9"] {
463+
if _swiftObject.hasPrefix(prefix) {
464+
return true
465+
}
466+
}
467+
return false
463468
}
464469
}
465470

TestFoundation/TestNSString.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TestNSString : XCTestCase {
2121

2222
var allTests : [(String, () -> ())] {
2323
return [
24+
("test_boolValue", test_boolValue ),
2425
("test_BridgeConstruction", test_BridgeConstruction ),
2526
("test_isEqualToStringWithSwiftString", test_isEqualToStringWithSwiftString ),
2627
("test_FromASCIIData", test_FromASCIIData ),
@@ -34,6 +35,17 @@ class TestNSString : XCTestCase {
3435
("test_FromMalformedNullTerminatedCStringInUTF8", test_FromMalformedNullTerminatedCStringInUTF8 ),
3536
]
3637
}
38+
39+
func test_boolValue() {
40+
let trueStrings: [NSString] = ["t", "true", "TRUE", "tRuE", "yes", "YES", "1"]
41+
for string in trueStrings {
42+
XCTAssert(string.boolValue)
43+
}
44+
let falseStrings: [NSString] = ["false", "FALSE", "fAlSe", "no", "NO", "0", "<true>", "_true"]
45+
for string in falseStrings {
46+
XCTAssertFalse(string.boolValue)
47+
}
48+
}
3749

3850
func test_BridgeConstruction() {
3951
let literalConversion: NSString = "literal"

0 commit comments

Comments
 (0)