Created
October 28, 2019 14:30
-
-
Save pilky/81bf837d25f39b9615b4b3e2b8231c8d to your computer and use it in GitHub Desktop.
Optional KeyPath Weirdness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class Foo { | |
var string = "test" | |
var optionalString: String? | |
} | |
let foo = Foo() | |
foo[keyPath: \.string] == nil //false | |
foo[keyPath: \.optionalString] == nil //true | |
func value<Object, Type>(of keyPath: ReferenceWritableKeyPath<Object, Type>, for object: Object) -> Any? { | |
return object[keyPath: keyPath] | |
} | |
value(of: \.string, for: foo) == nil // false | |
value(of: \.optionalString, for: foo) == nil // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment