Scott Zhu
Scott Zhu

Reputation: 8461

Access enum value defined in another class

I have a enum defined as follow,

class Question: NSObject {
    enum Type:String {
        case Text = "TEXT"
        case Image = "IMAGE"
    }
    /**!!!Here I can access it like this!!!*/
    var type = Type.Text
}

However, in another class,

class MyViewController: UIViewController {
    /**!!!This doesn't work!!!*/
    var type = Question.Type.Text
}

Are there anything that I did wrong?

Thanks

Upvotes: 2

Views: 1309

Answers (1)

RM_B
RM_B

Reputation: 101

Besides the aforementioned stuff (rename your enum!), you simply have to declare the enum outside of your Question class. You don't have to make a new file for this (although you can if you want), simply put it above your class like so:

enum QuestionType: String {
    case Text  = "TEXT"
    case Image = "IMAGE"
}

class Question: NSObject {
    //...
}

Now you can use the QuestionType enum in your MyViewController class.

Upvotes: 2

Related Questions