我想对我的 3 个案例进行切换,但我遇到了无法解决的错误:
错误:在类型名称之后需要成员名称或构造函数调用
自从我一直在使用类似的代码以来,一定有什么地方被我忽略了。但是现在我的项目几乎是空的,无法弄清楚出了什么问题?
import UIKit
class ViewController: UIViewController {
enum MyStateStatus {
case Ready
case Running
case Stopped
}
@IBAction func actionPressed(sender: UIButton) {
switch MyStateStatus {
case MyStateStatus.Ready:
print("I'm ready")
default:
print("other")
}
}
我正在使用 Swift,Xcode 6.3.2
更新:为了与 Swift 2.2 和 Xcode 7 兼容,将 println()
替换为 print()
。
最佳答案
在您的示例中,您正在将开关应用于枚举声明本身,但您必须切换一个包含可能的枚举值之一的对象。
例如:
var currentState: MyStateStatus = .Ready
@IBAction func actionPressed(sender: UIButton) {
switch currentState {
case .Ready:
println("I'm ready")
default:
println("other")
}
}
关于ios - 开关错误 : Expected member name or constructor call - what's wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31113337/