Gian
Gian

Reputation: 1213

Enums in Swift 2

I have questions regarding the enums in Swift that I could not give details of other topics.

Situation The properties must be accessed from any design class in the project.

The best practice is: One archive to a enum?

//  MessageEnum.swift
//  ProjectName

import UIKit

enum MessageEnum: String {
    case DEFAULT = "Default Message",
    USER = "User Message",
    SYSTEM = "System Message"
}

One class to a enum?

//  MessageEnum.swift
//  ProjectName

import UIKit

class messageEnumClass {

    enum MessageEnum: String {
        case DEFAULT = "Default Message",
        USER = "User Message",
        SYSTEM = "System Message"
    }

}

One NSObject with Enum?

//  MessageEnum.swift
//  ProjectName

import UIKit

class messageEnumClass: NSObject {

    enum MessageEnum: String {
        case DEFAULT = "Default Message",
        USER = "User Message",
        SYSTEM = "System Message"
    }

}

Based on the examples, I wonder what is the best practice when using enums in Swift.

Upvotes: 0

Views: 157

Answers (2)

ion-
ion-

Reputation: 1

I like this version which is a variation of your first example:

//  MessageEnum.swift
//  ProjectName

import UIKit

enum MessageEnum: String {
    case DEFAULT = "Default Message"
    case USER = "User Message"
    case SYSTEM = "System Message"
}

Upvotes: 0

NRitH
NRitH

Reputation: 13903

Although I really don't understand what you mean by "one archive to a enum", option 1 is the best practice. Putting the enum in a struct or class that exists only to hold that enum adds a level of redundancy that you should avoid.

Upvotes: 3

Related Questions