我目前正在玩swift。我正在关注Lynda.com手表套件教程,该教程通过创建一个简单的手表计算器来引导观众。
以下方法抛出错误"一行上的连续语句必须用';'""
分隔 func updateText(){
guard let labelInt:Int64 = Int64(labelString) else {
label.setText("Number is too big")
return
}
label.setText("\(labelInt)")
}
由于某些原因,xCode不识别保护,因此该语句会抛出错误。我看过多个使用后卫的教程,我错过了什么?该教程似乎使用完全相同的方法字符到字符没有问题。
整个文件供参考:
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
var labelString:String = "0"
@IBOutlet var label: WKInterfaceLabel!
@IBAction func tapped0(){tappedNumber(0)}
@IBAction func tapped1(){tappedNumber(1)}
@IBAction func tapped2(){tappedNumber(2)}
@IBAction func tapped3(){tappedNumber(3)}
@IBAction func tapped4(){tappedNumber(4)}
@IBAction func tapped5(){tappedNumber(5)}
@IBAction func tapped6(){tappedNumber(6)}
@IBAction func tapped7(){tappedNumber(7)}
@IBAction func tapped8(){tappedNumber(8)}
@IBAction func tapped9(){tappedNumber(9)}
func tappedNumber(num:Int){
labelString = labelString.stringByAppendingString("\(num)")
updateText()
}
func updateText(){
guard let labelInt:Int64 = Int64(labelString) else {
label.setText("Number is too big")
return
}
label.setText("\(labelInt)")
}
@IBAction func tappedPlus(){
}
@IBAction func tappedMinus(){
}
@IBAction func tappedClear(){
}
@IBAction func tappedEquals(){
}
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}
答案 0 :(得分:1)
guard
表达式仅在Swift 2 / Xcode 7中可用
Xcode 7的最终版本已于几天前(9月17日)发布。