Unwrapping Swift optional without variable reassignment

后端 未结 2 485
北荒
北荒 2020-12-15 13:55

When using optional binding to unwrap a single method call (or optional chaining for a long method call chain), the syntax is clear and understandable:

if le         


        
2条回答
  •  悲&欢浪女
    2020-12-15 14:35

    Here's the only alternative I'm aware of.

    func someFunction(childTitle: String?) {
        if childTitle != nil {
            ...
        }
    }
    

    But then childTitle is still an optional, so you would have to unwrap it every time you use it: childTitle!.doSomething(). It shouldn't affect performance, though.

提交回复
热议问题