小编Sir*_*rCJ的帖子

使用Swift4 Codable解码数组

我正在玩Swift 4 Codable,当我认为我已经掌握了它时,我从响应中解码了Weather键.

 let jsonData = """
    {"coord":{"lon":-113,"lat":35},
"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],
"base":"stations",
"main":{"temp":291.15,"pressure":1022,"humidity":72,"temp_min":291.15,"temp_max":291.15},
"visibility":16093,"wind":{"speed":3.6,"deg":200},
"clouds":{"all":1},
"dt":1503294780,"sys":{"type":1,"id":321,"message":0.184,"country":"US","sunrise":1503320222,"sunset":1503367937},
"id":5308281,"name":"Paulden","cod":200}
"""
Run Code Online (Sandbox Code Playgroud)

Swift模型:

//Top Level Container
struct Response: Codable {

    enum ResponseKeys: String, CodingKey {
        case name
        case code = "cod"
        case main
        case weather
    }
    //Nested Level Keys: Response > Weather
    enum WeatherKeys: String, CodingKey {
        case weather
    }
    //Nested Level Keys: Response > Main
    enum MainKeys: String, CodingKey {
        case temp
        case pressure
        case humidity
        case tempMin = "temp_min"
        case tempMax = "temp_max" …
Run Code Online (Sandbox Code Playgroud)

arrays json swift4 codable

4
推荐指数
1
解决办法
3665
查看次数

标签 统计

arrays ×1

codable ×1

json ×1

swift4 ×1