我有一個非常簡單的例子,不知道爲什麼它不能按預期工作。爲什麼我的可選值沒有解開,Swift?
var list:Array<Int> = [1,2,3,4,5]
var item:Int?
for var index = 0; index < list.count; index++ {
item! = list[index]
item = item + 5 // <-- error value of optional type 'Int?' not unwrapped
}
爲什麼斯威夫特力量我寫:item = item! + 5
我在這裏把它拆開:item! = list[index]
,如果列表返回nil
- 異常將被拋出。
據我所知這一步AE:item! = list[index]
的item
不nil
我試了幾個選項,如:
item! = list[index] as Int!
item! = list[index] as AnyOblect! as? Int
但仍然得到同樣的需求寫item = item! + 5
我用操場
即使我像'item!'一樣解開它之前? – snaggs 2014-09-13 14:09:08
我做了一個修改,請再次閱讀我的答案,@fessy。 – 2014-09-13 14:26:40