ios - 开关错误 : Expected member name or constructor call - what's wrong?

标签 ios swift enums

我想对我的 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/

相关文章:

java - 从枚举访问父类(super class)变量

swift - GCDAsyncUdpSocket 套接字在发送 255 个数据包之间关闭

swift - 如何在 Swift 数组中存储闭包

ios - 更新 UILabel 文本动画

ios - Swift:如何将经常使用的代码放在一个地方以便重用?

ios - UIWebView 开始加载但从未完成

dictionary - 我在 Xcode 6 beta5 中使用枚举键在我的字典上出现 EXC_BAD_INSTRUCTION 错误

c# - 创建具有键值作为字符串的枚举

ipad - UISwipeGestureRecognizer、UITapGestureRecognizer 和 MPMoviePlayerController 的问题 :-(

ios - 用于移动聊天应用程序的 Socket.io 与 xmpp