Swift Error fatal error: unexpectedly found nil while unwrapping an Optional value
致命的错误: 对一个为空的optional值进行解包.
强制解包(forced unwrapping): 在optional后面加叹号. 比方 "value!"
错误代码:
let testString ="lalala"
let a = testString.toInt();
println("\(a!)")
不能对为空的optional进行解包,否则会报执行时错误.所以在对optional进行解包之前进行推断是否为空.
正确写法:
let testString ="lalala"
let a = testString.toInt();
if (a != nil) {
println("\(a!)")
}