我正在学习JSONParsing。我按照教程进行操作,得到的是:guardleturl=URL(string:"http://localhost/test-api/public/api/register")else{return}varrequest=URLRequest(url:url)request.httpMethod="POST"letnewUser=User.init(name:self.collectionTF[0].text,email:self.collectionTF[1].text,password:self.collectionTF[2].text)do{letj
我在我的项目中使用免费日期API。我正在使用Decodable来解析JSON数据。我在这里创建了我的结构:-structjsonStruct:Decodable{varmessage:Bool?vardata:[dateData]}structdateData:Decodable{varquarter:Int?varday:String?varmonth:String?}这是我使用解码器的代码:-letjsonUrlString="https://api.lrs.org/random-date-generator?lim_quarters=40&source=api-docs"guar
Swift3改变了NSCoder的工作方式。正如其他SO问题所提到的,要解码Int或Bool等值类型,您必须使用特定函数。例如,decodeInteger用于解码Int值,如下所示:letvalue=decodeInteger(forKeykey:TestKey)但是,如果decodeInteger返回的值是String或Bool或Int以外的值怎么办?或者如果TestKey实际上映射为空,因为它包含错误的key数据怎么办?如何优雅地捕获这些错误? 最佳答案 在非整数键上使用decodeInteger会引发异常。遗憾的是,这是一个S
注意:这个问题我已经看过了->HowdoIusecustomkeyswithSwift4'sDecodableprotocol?但它没有解释如何编码/解码枚举这是我想要的结构:structMyStruct:Decodable{letcount:PostType}enumPostType:Decodable{casefast(value:Int,value2:Int)caseslow(string:String,string2:String)}现在我知道我希望我的结构看起来如何,问题是:我不知道init函数在PostType枚举中应该是什么样子。我使用下面的代码来帮助我快速构建JSON。
这个问题在这里已经有了答案:Fatalerror:DictionarydoesnotconformtoDecodablebecauseAnydoesnotconformtoDecodable(2个答案)关闭4年前。我正在尝试实现一个Decodable来解析json请求,但json请求在对象内部有一个字典。这是我的代码:structmyStruct:Decodable{letcontent:[String:Any]}enumCodingKeys:String,CodingKey{casecontent="content"}但是我收到了这个错误:类型“MyClass.myStruct”不符
letjsonString="""{"name":1,"gender":"male",}"""structPerson:Codable{varname:Stringvargender:Stringpublicinit(fromdecoder:Decoder)throws{do{letcontainer=trydecoder.container(keyedBy:CodingKeys.self)name=trycontainer.decode(String.self,forKey:.name)gender=trycontainer.decode(String.self,forKey:.ge
我已阅读HowtodecodeanestedJSONstructwithSwiftDecodableprotocol?它没有解决我将字符串文字数字值用作根字典的特定用例。还有HowtodecodeanestedJSONstructwithSwiftDecodableprotocol?ImanouPetit的回答。Can'tdecodeJSONdatafromAPILeoDabus的回答。货币本身就是字典,由data字典中的文字字符串数字表示,所以这让我很反感。我正在寻找使用枚举的最多的Swifty4模型,在其中可以很容易地看到哪些容器对应于哪些词典。附注DavidBerry给出了一个很
假设我有这样一个结构:structResult:Decodable{letanimal:Animal?}像这样的枚举:enumAnimal:String,Decodable{casecat="cat"casedog="dog"}但是返回的JSON是这样的:{"animal":""}如果我尝试使用JSONDecoder将其解码为Result结构,我会得到CannotinitializeAnimalfrominvalidStringvalue作为一个错误信息。我如何正确地将此JSON结果解码为动物属性为nil的Result? 最佳答案
我正在解析需要从服务器上的字典(这是一种遗留数据格式)转换为客户端简单的字符串数组的响应。因此,我想将名为“数据”的键解码为字典,这样我就可以遍历键并在客户端创建一个字符串数组。init(fromdecoder:Decoder)throws{letvalues=trydecoder.container(keyedBy:CodingKeys.self)do{letsome_data_dictionary=tryvalues.decode([String:Any].self,forKey:CodingKeys.data)for(kind,values)insome_data_diction
我正在尝试实现Swift4的新Decodable协议(protocol),但遇到了一些困难。这是我的JSON服务器响应:{"success":true,"errorCode":0,"message":"Succcess","data":{"name":"LoganHowlett","nickname":"TheWolverine","image":"http://heroapps.co.il/employee-tests/ios/logan.jpg","dateOfBirth":1880,"powers":["AdamantiumBones","Self-Healing","Adama