jjzjj

json - 使用 Codable 协议(protocol)解析包含日期作为键的 Stock API

我正在尝试解析来自AlphaVantage的股票api。这是一个响应的样子:APIResponseDemo我设置了四个类用于解码和编码:股票元数据时间序列高开低收我认为问题出在时间序列类中,因为我想获取一个数组日期作为键,每个日期都包含开盘价、收盘价、高价、低值。这三个类都符合Codable协议(protocol)。我更改了枚举中​​键的值,以便它可以匹配正确的JSONResponse。varstocks=[Stocks]()overridefuncviewDidLoad(){super.viewDidLoad()view.backgroundColor=.redloadURL()}f

swift - Codable 导致循环

我有这些类型的类(class):importUIKitclassReport:Codable{varname:String=""varbudgetLines:[ReportLines]=[]init(){self.name="MyReport"self.budgetLines=[ReportLines(name:"Test1",parent:self),ReportLines(name:"Test2",parent:self),ReportLines(name:"Test3",parent:self)]}}classReportLines:Codable{varname:Stringw

json - swift 4 : how to implement codable protocol for json array of different types

有人可以启发我如何为以下多种类型的json数组实现codableprotocol到swift结构吗?在下面的json素材数组中,可以是反射对象、视频对象或笔记对象。{"materials":[{"reflection":{"title":"3-2-1reflection","description":"pleasereflectaftertoday","questions":[{"question":"question1","answer":"answer1","answerImageUrl":"http://xxx"},{"question":"question1","answer"

ios - EXC_BAD_ACCESS 尝试编码 Codable-conformant 类的子类时

我一定是个白痴之类的,但我已经连续第三天摸不着头脑了,无法弄清楚我打算在我的Swift程序中编码一些JSON数据的意图出了什么问题......情况是这样的:我有两个类如下:classNode:Codable{//Nothinginhere}和classShape:Node{//Nocodeheretoo}然后,我尝试按如下方式对子类进行编码:do{letencodedData=tryJSONEncoder().encode(Shape())}catch{print(error)}这就是我添加到一个空的单View应用程序项目中的所有内容。当我运行它时,出现“Thread1:EXC_BAD

json - 如何在 Swift 4 中使用 codable 解码 JSON?

我在使用Codable解码JSON时遇到了一个大问题我得到了错误Thread1:Fatalerror:'try!'expressionunexpectedlyraisedanerror:Swift.DecodingError.keyNotFound(SpecieKeys(stringValue:"v",intValue:nil),Swift.DecodingError.Context(codingPath:[_JSONKey(stringValue:"Index0",intValue:0)],debugDescription:"NovalueassociatedwithkeySpeci

swift - 如何实现上下文可控的Swift Codable Encode函数?

我正在尝试为Codable结构实现一个编码器,我可以通过传入一个上下文来控制它,该上下文确定我将哪些字段添加到输出JSON中。但我想不出一个好的方法。例如假设我有以下内容:importFoundationstructMyStruct:Encodable{vara:Int=0varb:Int=0enumCodingKeys:String,CodingKey{caseacaseb}enumContext{casesummary,full}//Icanwritesomethinglikethisfuncencode(toencoder:Encoder)throws{varcontainer=

swift - 可以同时实现 Void 和 Codable 的泛型

这里我有一个用于api结果的结构structResponse{letstatus:Intletmessage:Stringletresult:Result}现在通常让这个符合Codable将意味着Result对象需要是Codable.这样做看起来像以下之一structResponse:Codable{...}//orextensionResponse:CodablewhereResult:Codable{}我遇到的问题是某些响应没有resultkey,我希望能够使用Response与Void类似的对象改为输入Response很像这样soquestion.目前我有一个可能的方法来解决这个

arrays - iOS Struct Codable 和过滤器?

我想通过startYear搜索特定的美国总统,我试图在Swift4中使用过滤器。我无法让它工作。我是故意使用Codable协议(protocol)的。structUSPresidents:Codable{letFName:StringletLName:StringletstartYear:IntletendYear:Int}letwidroww=USPresidents(FName:"Widrow",LName:"Wilson",startYear:1913,endYear:1921)letfdr=USPresidents(FName:"Frankly",LName:"Roosevel

Swift 4 JSONEncoder 不能对 String 或 Int 进行编码,但它们遵循 Codable 协议(protocol)

letencoder=JSONEncoder()do{letencodData=tryencoder.encode("teststring")//sameasInttypeprint(encodData)//nil}catchleterr{print(err.localizedDescription)//Thedatacouldn’tbewrittenbecauseitisn’tinthecorrectformat.}如何编码这些类型值 最佳答案 顶级(根)JSON对象只能是数组或字典。例如:do{letencoder=JSONEn

使用 codable 的 Swift JSON 解码找到了一个字符串/数据

我正在使用纽约时报API并收到消息typeMismatch(Swift.Array,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:"results",intValue:nil),_JSONKey(stringValue:"Index3",intValue:3),CodingKeys(stringValue:"multimedia",intValue:nil)],debugDescription:"ExpectedtodecodeArraybutfoundastring/datainstead.",under