Creating class for defining constants in swift

Note: I want to achieve similar functionality in swift - Where to store global constants in an iOS application?

I have two classes - MasterViewController and DetailViewController

I want to define an enum (refer below enum) and use its values in both classes:

enum Planet: Int {
    case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}

I tried to define it in MasterViewController and use it in DetailViewController like this:

let aPlanet = Planet.Earth

but compiler is complaining :(

"Use of unresolved identifier Planet"

Generally in my objective c code I used to have a global constant file, which I used to import in app-prefix.pch file, which was making it accessible to all files within my project, but in this case I am clueless.

Answers


If your enum is being defined in a class like this:

class MyClass {
    enum Planet: Int {
        // ...
    }
}

You have to access it through your class:

var aPlanet = MyClass.Planet.Earth

You also want to use the rawValue property. You will need that to access the actual Int value:

var aPlanet = MyClass.Planet.Earth.rawValue

In swift you can declare an enum, variable or function outside of any class or function and it will be available in all your classes (globally)(without the need to import a specific file).


Need Your Help

Workflow Design Dilemma - State Machine, yes or no

c# .net workflow workflow-foundation

I'm a beginner with WF, but I've read a book and done a lot of googling. I want to write an inventory management service. The inventory is made up of individual items which have a state:

REPL Environment for the Web

php javascript jquery ajax read-eval-print-loop

I'm looking to find a REPL system that can be executed on a web page and that the server can react to. Is there anything out there (I'd assume it would have to be using Javascript/AJAX)? If there's...