草庐IT

ios - swift 4 可编码 : Converting JSON return String to Int/Date/Float

我正在研究一些项目并删除JSON解析框架,因为使用Swift4似乎很简单。我遇到了这个奇怪的JSON返回,其中Ints和Dates作为Strings返回。我看了GrokSwift'sParsingJSONwithSwift4,Apple'swebsite,但我没有看到任何关于re:changingtypes的内容。Apple'sexamplecode显示了如何更改key名称,但我很难弄清楚如何更改key类型。这是它的样子:{"WaitTimes":[{"CheckpointIndex":"1","WaitTime":"1","Created_Datetime":"10/17/20176

ios - Swift 4 可解码 : The given data was not valid JSON

我正在尝试向我的本地服务器写入一个POST请求,这是我的功能:@IBActionfuncpostButtonAction(_sender:UIButton){guardleturl=URL(string:"http://localhost:443/api/message")else{return}varrequest=URLRequest(url:url)request.httpMethod="POST"request.addValue("application/json",forHTTPHeaderField:"Content-Type")print("POSTED")letdate

ios - Swift 4 可解码 : The given data was not valid JSON

我正在尝试向我的本地服务器写入一个POST请求,这是我的功能:@IBActionfuncpostButtonAction(_sender:UIButton){guardleturl=URL(string:"http://localhost:443/api/message")else{return}varrequest=URLRequest(url:url)request.httpMethod="POST"request.addValue("application/json",forHTTPHeaderField:"Content-Type")print("POSTED")letdate

json - 如何在 Swift 4 中实现 JSON 数据的多态解码?

我正在尝试根据API端点返回的数据呈现View。我的JSON看起来(大致)像这样:{"sections":[{"title":"Featured","section_layout_type":"featured_panels","section_items":[{"item_type":"foo","id":3,"title":"Bisbee1","audio_url":"http://example.com/foo1.mp3","feature_image_url":"http://example.com/feature1.jpg"},{"item_type":"bar","id":

json - 如何在 Swift 4 中实现 JSON 数据的多态解码?

我正在尝试根据API端点返回的数据呈现View。我的JSON看起来(大致)像这样:{"sections":[{"title":"Featured","section_layout_type":"featured_panels","section_items":[{"item_type":"foo","id":3,"title":"Bisbee1","audio_url":"http://example.com/foo1.mp3","feature_image_url":"http://example.com/feature1.jpg"},{"item_type":"bar","id":

json - Swift Codable 期望解码 Dictionary<String, Any> 但发现了一个字符串/数据

我一直在使用Codable协议(protocol)这是我的JSON文件:{"Adress":[],"Object":[{"next-date":"2017-10-30T11:00:00Z","text-sample":"Sometext","image-path":["photo1.png","photo2.png"],"email":"john.doe@test.com","id":"27"},{"next-date":"2017-10-30T09:00:00Z","text-sample":"TestTest","image-path":["image1.png"],"email"

json - Swift Codable 期望解码 Dictionary<String, Any> 但发现了一个字符串/数据

我一直在使用Codable协议(protocol)这是我的JSON文件:{"Adress":[],"Object":[{"next-date":"2017-10-30T11:00:00Z","text-sample":"Sometext","image-path":["photo1.png","photo2.png"],"email":"john.doe@test.com","id":"27"},{"next-date":"2017-10-30T09:00:00Z","text-sample":"TestTest","image-path":["image1.png"],"email"

swift - 如何在 swift 4 Codable 中手动解码数组?

这是我的代码。但我不知道将值设置为什么。它必须手动完成,因为实际结构比这个例子稍微复杂一些。有什么帮助吗?structSomething:Decodable{value:[Int]enumCodingKeys:String,CodingKeys{casevalue}init(fromdecoder:Decoder){letcontainer=trydecoder.container(keyedBy:CodingKeys.self)value=???// 最佳答案 由于一些错误/拼写错误,您的代码无法编译。解码Int数组structS

swift - 如何在 swift 4 Codable 中手动解码数组?

这是我的代码。但我不知道将值设置为什么。它必须手动完成,因为实际结构比这个例子稍微复杂一些。有什么帮助吗?structSomething:Decodable{value:[Int]enumCodingKeys:String,CodingKeys{casevalue}init(fromdecoder:Decoder){letcontainer=trydecoder.container(keyedBy:CodingKeys.self)value=???// 最佳答案 由于一些错误/拼写错误,您的代码无法编译。解码Int数组structS

Swift 4 Codable 数组的

所以我有一个返回JSON对象数组的API路由。例如:[{"firstname":"Tom","lastname":"Smith","age":31},{"firstname":"Bob","lastname":"Smith","age":28}]我正在尝试设想如何使用Swift中新的可编码功能将它们转换为一个类中的两个对象。因此,如果我有一个可编码的person类,我想接受该响应并让它给我两个person对象。我也在使用Alamofire来处理请求。我该怎么做?到目前为止,我所看到的与可编码内容相关的所有内容都只允许1个对象。而且我还没有看到与Alamofire或网络框架的任何集成。