Downcasting optionals in Swift: as? Type, or as! Type?

前端 未结 9 1691
耶瑟儿~
耶瑟儿~ 2020-11-29 17:32

Given the following in Swift:

var optionalString: String?
let dict = NSDictionary()

What is the practical difference between the following

9条回答
  •  隐瞒了意图╮
    2020-11-29 17:52

    It may be easiest to remember the pattern for these operators in Swift as: ! implies "this might trap," while ? indicates "this might be nil."

    refer to: https://developer.apple.com/swift/blog/?id=23

提交回复
热议问题