草庐IT

Decodable

全部标签

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 的 Decodable 解码 Void

我有一个通用的REST请求:structRequest{…}T是请求的返回类型,例如:structAnimal{…}letanimalRequest=Requestletanimal:Animal=sendRequest(animalRequest)现在我想表达泛型必须符合Decodable这样我就可以解码来自服务器的JSON响应:structRequestwhereT:Decodable{…}structAnimal:Decodable{…}这是有道理且有效的——直到我收到一个没有响应的请求,Request.编译器对此不满意:Type'Void'doesnotconformtopro

json - 使用 Swift 4 的 Decodable 解码 Void

我有一个通用的REST请求:structRequest{…}T是请求的返回类型,例如:structAnimal{…}letanimalRequest=Requestletanimal:Animal=sendRequest(animalRequest)现在我想表达泛型必须符合Decodable这样我就可以解码来自服务器的JSON响应:structRequestwhereT:Decodable{…}structAnimal:Decodable{…}这是有道理且有效的——直到我收到一个没有响应的请求,Request.编译器对此不满意:Type'Void'doesnotconformtopro

Swift 4 Decodable - 以枚举为键的字典

我的数据结构有一个枚举作为键,我希望下面的内容能够自动解码。这是错误还是某些配置问题?importFoundationenumAnEnum:String,Codable{caseenumValue}structAStruct:Codable{letdictionary:[AnEnum:String]}letjsonDict=["dictionary":["enumValue":"someString"]]letdata=try!JSONSerialization.data(withJSONObject:jsonDict,options:.prettyPrinted)letdecoder

Swift 4 Decodable - 以枚举为键的字典

我的数据结构有一个枚举作为键,我希望下面的内容能够自动解码。这是错误还是某些配置问题?importFoundationenumAnEnum:String,Codable{caseenumValue}structAStruct:Codable{letdictionary:[AnEnum:String]}letjsonDict=["dictionary":["enumValue":"someString"]]letdata=try!JSONSerialization.data(withJSONObject:jsonDict,options:.prettyPrinted)letdecoder

swift - 将 Decodable 与继承一起使用会引发异常

我正在针对RestAPI服务工作,其中响应分为基本响应,所有其他响应都继承自它。我正在尝试使用解码器接口(interface)为我的响应模型类构建相同的结构。但是我在解码继承类时遇到问题。我试图关注这个问题:UsingDecodableinSwift4withInheritance但没有运气。这是初始结构:classLoginResponse:BaseResponse{varMessage:String?privateenumCodingKeys:String,CodingKey{caseMessage}requiredinit(fromdecoder:Decoder)throws{l

swift - 如何在 Swift 中制作枚举 Decodable?

enumPostType:Decodable{init(fromdecoder:Decoder)throws{//Whatdoiputhere?}caseImageenumCodingKeys:String,CodingKey{caseimage}}我要做什么来完成这个?另外,假设我将case更改为:caseimage(value:Int)如何使它符合Decodable?这是我的完整代码(不起作用)letjsonData="""{"count":4}""".data(using:.utf8)!do{letdecoder=JSONDecoder()letresponse=trydecod

json - Swift Decodable 的可失败初始化器

我正在尝试解析以下json模式,海报可能为空也可能不为空{"poster":{},"recommends":[]}我的可解码类如下:publicstructRecommendedList:Decodable{publicletposter:Poster?publicletrecommends:[Recommend]}publicstructPoster:Decodable{publicletbackgroundImage:URLpubliclettopImage:URLpublicletwindowImage:URLpublicletwindowSkinImagePath:URLpub

json - Swift 4 Decodable 和换行符\n

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭4年前。ImprovethisquestioniTunes搜索API返回有时包含换行符(\n)的JSON。这使得解码失败。你可以在这里看到一个例子:curl“https://itunes.apple.com/search?term=Ruismaker&entity=software&media=so