Wenn Sie Wert von user_type
in Enum speichern wollen:
Ändern Sie Ihre Enum-Variablentyp in string:
enum UserType: String {
case basic = "basic"
case pro = "pro"
}
Lösung, ohne JASON mit
Speichern Sie Ihre JSON in Wörterbuchtyp Variable:
let jsonVar = [
"first_name": "John",
"last_name": "Doe",
"user_type": "basic"
] as [String : Any]
nun einen Wert von Wörterbuch-Variable erhalten und Enum Variable Rohwert erstellen:
if let user_type = jsonVar["user_type"] as? String {
let usertype = UserType(rawValue: user_type)
}
Lösung, mit JASON
let json = [
"first_name": "John",
"last_name": "Doe",
"user_type": "basic"
] as AnyObject
let jasonVar = JSON(json)
if let user_type:String = jasonVar["user_type"].string {
let usertype = UserType(rawValue: user_type)
}
Was meinst du mit Parsing? Möchten Sie einen Wert 'user_type' vom JSON-Speicher in enum-Variable erhalten? – Krunal
ja, du hast Recht –