Swift 2.2打破了可选/解包可选 [英] Swift 2.2 breaks optionals/unwrapping optionals
问题描述
Swift 2.2几乎破坏了我所有的代码.甚至这个分配给标签的简单字符串也不再起作用:
Swift 2.2 has broken almost all my code. Even this simple string assigning to label doesn't work anymore:
cell.categoryName.text = peopleArray![indexPath.row]["Name"] as? String
错误显示为"从字符串"向下转换?!"到'String'仅解开可选内容,您是要使用'!!'吗?"
我现在要做什么更改.
编辑:
更多问题:
if (dict["data"]!["DataDetails"] as! NSArray).count == 0 {
}
由于这个原因,我遇到了分段错误,错误显示如下:警告:从字符串?!"强制转换无关类型"NSArray"总是失败
Due to this I am getting a segmentation fault and the error shows this: warning: cast from 'String?!' to unrelated type 'NSArray' always fails
更新:
我在我的课程中使用了NSDictionaries,NSArrays,这似乎引起了问题.将所有文字从Obj-C更改为swift,可使代码正常工作. 因此,我还将建议其他开发人员更喜欢快速文字.
I was using NSDictionaries, NSArrays in my classes that seems to cause the problem. Changing all the literals from Obj-C to swift made the code work properly. So, I will also recommend other developers to prefer swift literals.
推荐答案
使用swift类型和Objective-c NS ...类型似乎有些细微差异
it seems there are some slight differences when using the swift types and the objective-c NS... types
例如
let dic:NSDictionary? = ["a":"a"]
let str:NSString? = dic!["a"] as? NSString
let dic2:Dictionary? = ["b":"b"]
let str2:String? = dic2!["b"] //dont need to do any casting, already optional
print(str)
print(str2)
打印
Optional(a)
Optional("b")
因此,根据数组/字典的定义方式,您可能需要不同的转换/展开
so depending on how your array / dictionary is defined, you might need different casting/unwrapping
这篇关于Swift 2.2打破了可选/解包可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!