optional-chaining

dynamicType of optional chaining not the same as assignment

允我心安 提交于 2019-12-10 17:56:45
问题 Optional chaining returns always an optional value. To reflect the fact that optional chaining can be called on a nil value, the result of an optional chaining call is always an optional value, even if the property, method, or subscript you are querying returns a nonoptional value. The Swift Programming Language Why the heck does in a playground the type not optional? let stringOptEmpty: String? = "" stringOptEmpty?.isEmpty // is true stringOptEmpty?.isEmpty.dynamicType // Bool.Type But the

Optional chaining used in left side of assignment in Swift

∥☆過路亽.° 提交于 2019-12-10 15:15:03
问题 What does it mean when optional chaining is used in the left side of the assignment statement? Will the app crash if the optional variable is nil? e.g. // cell is a UITableViewCell cell.textLabel?.text = "Test" 回答1: Sort of like a short-circuiting && operator that stops when it reaches the first false value, optional chaining will stop when it hits the first nil value. So in an extreme case like container?.cell?.textLabel?.text = "foo" , any of container, cell, or textLabel could be nil. If

What is the difference when change to use optional chaining replace forced unwrapping in swift?

被刻印的时光 ゝ 提交于 2019-12-02 06:53:08
When call a function of an object instance, the object may be not exist(optional type), it seems like you can always put an question mark behind the object name, instead of put an exclamation mark behind the object name, and not crash. window!.rootViewController = containerViewController // forced unwrapping // Can change to question mark and not crash. window?.rootViewController = containerViewController // Optional chaining Is that in the place of use forced unwrapping, you can always change to use optional chaining, and the result is same? If yes, what is the difference? The difference is