首页 > 代码库 > Initializer for conditional binding must have Optional type, not 'String'

Initializer for conditional binding must have Optional type, not 'String'

今天看到问Swift问题:  Initializer for conditional binding must have Optional type, not ‘String‘

以前没遇到过这个问题,这不赶紧写个demo验证一下,弄懂什么问题吧,算是积累小知识了.

技术分享

报错原因:

if 里面的判断类型必须是Optional类型。

解决方法:

技术分享

注意不仅是if let会报错,guard let 同样是这样...

Initializer for conditional binding must have Optional type, not 'String'