2017-09-03 59 views
1

我有一個EmailVerificationStatus枚舉與該符合Codable協議的String相關類型:如何將CodingKeys用於符合Codable協議的枚舉?

enum EmailVerificationStatus: String, Codable { 
    case unverified 
    case verified 
} 

我正在與工作的web服務發送的那些情況下大寫(UNVERIFIED/VERIFIED)。我如何使用CodingKeys枚舉來映射這種差異?像下面的東西不起作用:

enum CodingKeys: String, CodingKey { 
    case unverified = "UNVERIFIED" 
    case verified = "VERIFIED" 
} 

回答

1

好的。那很簡單。無CodingKeys需要:

enum EmailVerificationStatus: String, Codable { 
    case verified = "VERIFIED" 
    case unverified = "UNVERIFIED" 
} 
0

我會建議你使用結構的電子郵件...類型和嵌套你的結構中枚舉CodingKeys。 CodingKeys允許您將結構變量與您的源數據案例(來自webservice)進行映射。

struct EmailVerificationStatus: String, Codable { 
     var unverified: String 
     var verified: String 

     enum CodingKeys: String, CodingKey { 
      case unverified = "UNVERIFIED" 
      case verified = "VERIFIED" 
     } 
    } 
相關問題