2017-06-09 9 views
2

ich eine Enum haben, die wie folgt aussieht:Erhalten Index von ENUM mit der Erweiterung von String,

enum Status: String { 
    case online = "online" 
    case offline = "offline" 
    case na = "na" 
} 

ich den String-Wert müssen und ich weiß, wie es zu bekommen, aber meine Frage ist, ob Es ist möglich um auch den Indexwert für jeden Fall in der Enumeration zu erhalten.

0 für online, 1 für offline und 2 für na.

Ich werde in Zukunft weitere Statuen hinzufügen.

+1

Vergleichen [einen String Enum von Index Zugriff] (https://stackoverflow.com/questions/39305150/accessing-a-string-enum-by-index). –

Antwort

0

Wie wäre es damit.

enum Status: Int { 
    case online = 0 
    case offline = 1 
    case na = 2 
} 

Sie können sowohl den String-Wert als auch den Integer-Index erhalten.

// enum as string 
let enumName = "\(Status.online)" // `online` 

// enum as int value 
let enumValue = Status.online.rawValue // 0 

// enum from int 
let enumm = Status.init(rawValue: 1) 

Ich hoffe, es hilft. Vielen Dank.

0

Wie enum in Swift hat keinen Index der Werte (‚s Kommentar, müssen Sie sich selbst etwas schaffen Sie sich bitte an den Pfosten in Martin R lesen)‚index‘Funktion oder alle Werte in ein Array abzubilden habe den Index.

Sie können wie in diesem Beitrag oder eine andere Art und Weise implementieren zu tun:

enum Status: String { 
    case online = "online" 
    case offline = "offline" 
    case na = "na" 

    static func index(of aStatus: Status) -> Int { 
     let elements = [Status.online, Status.offline, Status.na] 

     return elements.index(of: aStatus)! 
    } 

    static func element(at index: Int) -> Status? { 
     let elements = [Status.online, Status.offline, Status.na] 

     if index >= 0 && index < elements.count { 
      return elements[index] 
     } else { 
      return nil 
     } 
    } 
} 

let a = Status.na 

//return 2 
let index = Status.index(of: a) 

//return Status.offline 
let element2 = Status.element(at: 1) 

//return nil 
let element3 = Status.element(at: 3) 
0

ich diese Erweiterung bin mit:

protocol EnumIterable: RawRepresentable { 

    static var allValues: [Self] { get } 
    var index: Int? { get } 
} 

extension EnumIterable { 

    static var count: Int { 
     return allValues.count 
    } 
} 

extension EnumIterable where Self.RawValue: Equatable { 

    var next: Self? { 
     if let index = Self.allValues.index(where: { rawValue == $0.rawValue }) { 
      return Self.allValues[safe: index + 1] 
     } 
     return nil 
    } 

    var index: Int? { 
     return Self.allValues.index { rawValue == $0.rawValue } 
    } 
} 

Aber Sie würden allValues ​​Variable definieren:

enum Status: String, EnumIterable { 

    case online = "online" 
    case offline = "offline" 
    case na = "na" 

    static var allValues: [Status] { 
     return [ 
      .online, 
      .offline, 
      .na, 
     ] 
    } 
} 

Ähnliches wurde hier gelöst (Anzahl der Aufzählungen): How do I get the count of a Swift enum?

Nächste Möglichkeit ist Enumeration wie folgt zu definieren:

enum Status: Int { 

    case online = 0 
    case offline 
    case na 

    var index: Int { 
     return rawValue 
    } 
    var value: String { 
     return String(describing: self) 
    } 
} 

print (Status.online.value) // online 
print (Status.online.index) // 0 

oder

enum Status: Int { 

    case online = 0 
    case offline 
    case na 

    var index: Int { 
     return rawValue 
    } 
    var value: String { 
     switch self { 
     case .online: 
      return "online" 
     case .offline: 
      return "offline" 
     case .na: 
      return "na" 
     } 
    } 
} 

print (Status.online.value) // online 
print (Status.online.index) // 0 

Hinweis: für die Definition String-Wert, Sie CustomStringConvertible-Protokoll verwenden kann.

ZB:

enum Status: Int, CustomStringConvertible { 

    case online = 0 
    case offline 
    case na 

    var index: Int { 
     return rawValue 
    } 
    var description: String { 
     switch self { 
     case .online: 
      return "online" 
     case .offline: 
      return "offline" 
     case .na: 
      return "na" 
     } 
    } 
}