我有一个枚举定义如下,
class Question: NSObject {
enum Type:String {
case Text = "TEXT"
case Image = "IMAGE"
}
/**!!!Here I can access it like this!!!*/
var type = Type.Text
}
但是,在另一个类(class)中,
class MyViewController: UIViewController {
/**!!!This doesn't work!!!*/
var type = Question.Type.Text
}
我有什么做错的地方吗?
谢谢
最佳答案
除了上述内容(重命名您的枚举!),您只需在 Question
类之外声明该枚举即可。您不必为此创建一个新文件(尽管如果您愿意也可以),只需将其放在您的类之上,如下所示:
enum QuestionType: String {
case Text = "TEXT"
case Image = "IMAGE"
}
class Question: NSObject {
//...
}
现在您可以在 MyViewController
类中使用 QuestionType
枚举。
关于ios - 访问另一个类中定义的枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33580630/