Bridge Optional to NSNull (#4057)

This commit is contained in:
Jaden Geller
2016-09-07 18:50:37 -07:00
committed by GitHub
parent ebd9e408fb
commit af1c1f11ed
2 changed files with 26 additions and 4 deletions

View File

@@ -531,8 +531,11 @@ class RealmTests: TestCase {
realm.createObject(ofType: type, populatedWith: ["b", 2])
}
let object1 = realm.object(ofType: type, forPrimaryKey: NSNull())
XCTAssertNotNil(object1)
let object1a = realm.object(ofType: type, forPrimaryKey: NSNull())
XCTAssertNotNil(object1a)
let object1b = realm.object(ofType: type, forPrimaryKey: nil as Wrapped?)
XCTAssertNotNil(object1b)
let object2 = realm.object(ofType: type, forPrimaryKey: 2 as Wrapped)
XCTAssertNotNil(object2)
@@ -1270,8 +1273,11 @@ class RealmTests: TestCase {
realm.create(type, value: ["b", 2])
}
let object1 = realm.objectForPrimaryKey(type, key: NSNull())
XCTAssertNotNil(object1)
let object1a = realm.objectForPrimaryKey(type, key: NSNull())
XCTAssertNotNil(object1a)
let object1b = realm.objectForPrimaryKey(type, key: nil as Int?)
XCTAssertNotNil(object1b)
let object2 = realm.objectForPrimaryKey(type, key: 2)
XCTAssertNotNil(object2)

View File

@@ -113,6 +113,22 @@ extension Int64: CustomObjectiveCBridgeable {
return NSNumber(value: self)
}
}
extension Optional: CustomObjectiveCBridgeable {
static func bridging(objCValue: Any) -> Optional {
if objCValue is NSNull {
return nil
} else {
return .some(dynamicBridgeCast(fromObjectiveC: objCValue))
}
}
var objCValue: Any {
if let value = self {
return value
} else {
return NSNull()
}
}
}
#else