首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swift JSONDecoder不工作

swift JSONDecoder不工作
EN

Stack Overflow用户
提问于 2017-06-15 00:35:32
回答 0查看 6.1K关注 0票数 4

我观看了WWDC 102并尝试使用JSONDecoder进行建模,但我有一个问题,这是我的结构

代码语言:javascript
复制
public struct DataListResult:Codable{
    let _id : String
    let createdAt : String
    let desc : String
    let images : Array<String>
    let publishedAt : String
    let source : String
    let type : String
    let url : String
    let used : Int
    let who : String
}

public struct DataListModel:Codable{

    let results : [DataListResult]
    let error: Bool
}

JSON:

代码语言:javascript
复制
{
  "results" : [
    {
      "_id" : "59266a79421aa92c73b6475c",
      "images" : [
        "http:\/\/img.gank.io\/875a9508-3a1e-4d4b-8b91-c111ea62871a"
      ],
      "source" : "chrome",
      "who" : "S",
      "publishedAt" : "2017-05-25T13:32:48.92Z",
      "used" : true,
      "createdAt" : "2017-05-25T13:24:09.35Z",
      "type" : "iOS",
      "desc" : "iOS ",
      "url" : "https:\/\/github.com\/adamzjk\/iOS-ObjectDetection"
    },
    {
      "_id" : "592502d6421aa92c769a8bac",
      "images" : [
        "http:\/\/img.gank.io\/44e8aa0a-b66f-4a5b-9cb0-74c3ae9fc156"
      ],
      "source" : "chrome",
      "who" : "S",
      "publishedAt" : "2017-05-24T12:09:25.526Z",
      "used" : true,
      "createdAt" : "2017-05-24T11:49:42.14Z",
      "type" : "iOS",
      "desc" : "Whatʼs new in Swift 4",
      "url" : "https:\/\/github.com\/ole\/whats-new-in-swift-4"
    },
    {
      "_id" : "5923a438421aa92c73b64743",
      "images" : [
        "http:\/\/img.gank.io\/25762b53-b2ba-4c1c-9666-1683cd72bb82"
      ],
      "source" : "chrome",
      "who" : "who",
      "publishedAt" : "2017-05-23T11:14:05.141Z",
      "used" : true,
      "createdAt" : "2017-05-23T10:53:44.853Z",
      "type" : "iOS",
      "desc" : "music",
      "url" : "https:\/\/github.com\/HarshilShah\/DeckTransition"
    }
  ],
  "error" : false
}

属性images不能解码,因为我删除了它可以成功,但我不知道如何修复,请告诉我,谢谢

代码语言:javascript
复制
 do
            {
                let model = try JSONDecoder().decode(DataListModel.self, from: response.data!)

            }catch let error as NSError {

               print("\(error)") //Error Domain=Swift.DecodingError Code=2 "(null)"


            }

-上次更新2017.6.17

代码语言:javascript
复制
let images : Array<String> -> let images : Array<String>? 

图片为空时需要定语属性,感谢您的帮助

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44550235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档