Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Assorted optional chaining / binding examples
class A {
var tInt: Int?
var tDouble: Double?
var tString: String?
var tObj: B?
func chainTest() {
self.tObj?.tInt = 42
self.tObj?.tDouble = 3.1415
self.tObj?.tString = "Hello"
}
func letTest() {
if let concreteThing = self.tObj {
concreteThing.tInt = 42
concreteThing.tDouble = 3.1415
concreteThing.tString = "Hello"
}
}
}
class B {
var tInt: Int?
var tDouble: Double?
var tString: String?
}
func chainTest(testObj: A?) {
testObj?.tInt = 42
testObj?.tDouble = 3.1415
testObj?.tString = "Hello"
}
func letTest(testObj: A?) {
if let concreteThing = testObj {
concreteThing.tInt = 42
concreteThing.tDouble = 3.1415
concreteThing.tString = "Hello"
}
}
var testObjA : A? = A()
chainTest(testObjA)
letTest(testObjA)
testObjA?.chainTest()
testObjA?.letTest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment