jjzjj

objective-c - 如何在 Objective C 数据模型类中使用 Codable 协议(protocol)?

我正在混合搭配iOS源代码。我已经为swift数据模型类实现了可编码,这减少了编写解析器逻辑的负担。我试图使我的objective-c类符合可编码协议(protocol),这又引发了错误“找不到‘可编码’的协议(protocol)声明”。有没有办法将这个快速协议(protocol)用于objective-c类?或者是否有任何其他objective-capi提供与Codable相同的功能?这个想法是让swift和objectivec类的解析逻辑相同。 最佳答案 是的,您可以将Codable与Obj-C一起使用。棘手的部分是因为Obj-

ios - 如何保存实现 Codable 的自定义对象

现在使用Swift4可以更轻松地对JSONorPropertieslist进行编码/解码.但我找不到如何使用Codable编码数据,而不使用Objective-C方法initWithCoder和encodeWithCoder。考虑这个简单的类:structQuestion:Codable{vartitle:Stringvaranswer:Intvarquestion:Int}如何使用CodingKeys而不是initWithCoder和encodeWithCoder将其编码为数据?编辑:我还需要能够使用NSKeyedArchiver反序列化以前保存在userdefaults中的对象。

swift - 使用字典/数组初始化符合 Codable 的对象

我的主要用例是使用字典创建对象:例如structPerson:Codable{letname:String}letdictionary=["name":"Bob"]letperson=Person(from:dictionary)我想避免编写自定义实现并希望尽可能高效。 最佳答案 目前我拥有的最佳解决方案是这个,但它有编码/解码的开销。extensionDecodable{init(from:Any)throws{letdata=tryJSONSerialization.data(withJSONObject:from,option

json - 忽略 Codable 对象中的非 Codable 可选属性

当遵循Codable协议(protocol)时,我不能轻易跳过非Codable类的可选属性在Ride结构中,我们希望跳过driver属性的编码和解码,并保留它nil解码时:structRide:Codable{publicvarnumber:Stringpublicvarpassenger:Passenger?//Codableconformingpublicvardriver:Driver?//NSObjectsubclass,doesn'tconformtoCodableenumCodingKeys:String,CodingKey{casenumbercasepassenger}

swift 4 : With Codable `Generic parameter ' T' could not be inferred`

我收到以下错误:无法推断通用参数“T”线上:letdata=tryencoder.encode(obj)这是代码importFoundationstructUser:Codable{varfirstName:StringvarlastName:String}letu1=User(firstName:"Ann",lastName:"A")letu2=User(firstName:"Ben",lastName:"B")letu3=User(firstName:"Charlie",lastName:"C")letu4=User(firstName:"David",lastName:"D")l

ios - 如何使用 Swift Codable 处理部分动态的 JSON?

我收到了一些通过websocket连接传入的JSON消息。//samplemessage{type:"person",data:{name:"john"}}//someothermessage{type:"location",data:{x:101,y:56}}如何使用Swift4和Codable协议(protocol)将这些消息转换为适当的结构?在Go中,我可以做类似的事情:“嘿,目前我只关心type字段,我对其余部分不感兴趣(data部分)。”看起来像这样typeMessagestruct{Typestring`json:"type"`Datajson.RawMessage`jso

swift - 不符合 Decodable/Codable 协议(protocol)

我正在使用以下结构:structItem:Codable{varcategory:StringvarbirthDate:Datevarswitch:BoolvarweightNew:[Weight]varweightOld:ArrayvarcreatedAt:DatevaritemIdentifier:UUIDvarcompleted:BoolfuncsaveItem(){DataManager.save(self,with:itemIdentifier.uuidString)}funcdeleteItem(){DataManager.delete(itemIdentifier.uui

ios - 如何在 Codable 类型中使用 Any

我目前在我的项目中使用Codable类型并遇到问题。structPerson:Codable{varid:Any}上述代码中的id可以是String或Int。这就是id类型为Any的原因。我知道Any不是Codable。我需要知道的是如何让它发挥作用。 最佳答案 量子值首先,您可以定义一个可以从String和Int值中解码的类型。在这里。enumQuantumValue:Decodable{caseint(Int),string(String)init(fromdecoder:Decoder)throws{ifletint=try?

ios - Swift 4 中带有默认大小写的 Codable 枚举

我定义了一个enum如下:enumType:String,Codable{casetext="text"caseimage="image"casedocument="document"caseprofile="profile"casesign="sign"caseinputDate="input_date"caseinputText="input_text"caseinputNumber="input_number"caseinputOption="input_option"caseunknown}映射一个JSON字符串属性。自动序列化和反序列化工作正常,但我发现如果遇到不同的字符串,

swift - 如何在 Swift 中使用 Codable 转换带有可选小数秒的日期字符串?

我正在用Swift的Codable替换旧的JSON解析代码,但遇到了一些问题。我想这与其说是一个Codable问题,不如说是一个DateFormatter问题。从结构开始structJustADate:Codable{vardate:Date}和一个json字符串letjson="""{"date":"2017-06-19T18:43:19Z"}"""现在开始解码letdecoder=JSONDecoder()decoder.dateDecodingStrategy=.iso8601letdata=json.data(using:.utf8)!letjustADate=try!deco