小编Mis*_*sha的帖子

Swift 4使用Codable解码json

有人能告诉我我做错了什么吗?我在这里查看了所有问题,如下所示如何使用Swift Decodable协议解码嵌套的JSON结构?我发现了一个看起来正是我需要的Swift 4 Codable解码json.

{
"success": true,
"message": "got the locations!",
"data": {
    "LocationList": [
        {
            "LocID": 1,
            "LocName": "Downtown"
        },
        {
            "LocID": 2,
            "LocName": "Uptown"
        },
        {
            "LocID": 3,
            "LocName": "Midtown"
        }
     ]
  }
}

struct Location: Codable {
    var data: [LocationList]
}

struct LocationList: Codable {
    var LocID: Int!
    var LocName: String!
}

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: "/getlocationlist")

    let task = URLSession.shared.dataTask(with: url!) { data, …
Run Code Online (Sandbox Code Playgroud)

swift4 codable

7
推荐指数
1
解决办法
3402
查看次数

标签 统计

codable ×1

swift4 ×1