if let mathematicalSymbol = sender.currentTitle {
brain.performOperation(mathematicalSymbol)
}
上面的代码引入了下面的错误;
可选类型“字符串?”的值 未拆封;你的意思是用'!' 或者 '?'?
从这个屏幕截图中可以看出;
sender.currentTitle
是可选的。
以下是 Apple 的“ The Swift Programming Language (Swift 2.2) ”的摘录,其示例代码就在其下方;
如果可选值是
nil
,则条件是false
并且大括号中的代码被跳过。否则,可选值被解包并分配给常量 afterlet
,这使得解包值 在代码块内可用。
这是该摘录的示例代码;
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \(name)"
}
因此,出于这些原因,我认为要么我错过了某些东西,要么我遇到了一个错误。
我也在 Playground 上尝试过类似的东西,但没有出现类似的错误;
这是我的 Swift 版本;
Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)
Target: x86_64-apple-macosx10.9