You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import Foundation
// "XXX" prefix of each class of the applicationpublicclassXXXUser:Codable{publicenumCodingKeys:String,CodingKey{// Fields define on the jsoncase id
case name
case twitter
// Fields for flat versioncase profileUrl
case createdAt
}// Use only internally to extact sub-structsprivateclassTwitter:Codable{publicenumCodingKeys:String,CodingKey{case profileUrl ="profil_url"case createdAt ="created_at"}publicvarprofileUrl:String=""publicvarcreatedAt:Date=Date.distantPast
}publicvarid:Int16=0publicvarname:String=""publicvarprofileUrl:String=""publicvarcreatedAt:Date=Date.distantPast
publicinit(){}// MARK: -> Public protocol Encodablepublicfunc encode(to pEncoder:Encoder)throws{varlContainer= pEncoder.container(keyedBy:CodingKeys.self)try lContainer.encode(self.id, forKey:.id)try lContainer.encode(self.name, forKey:.name)try lContainer.encode(self.profileUrl, forKey:.profileUrl)try lContainer.encode(self.createdAt, forKey:.createdAt)}// MARK: -> Public protocol Decodablepublicrequiredinit(from pDecoder:Decoder)throws{
if let lContainer =try? pDecoder.container(keyedBy:CodingKeys.self){varlInvalidFields:[String]=[]// Required
if let lId =try? lContainer.decode(Int.self, forKey:.id){self.id = lId
}else{
lInvalidFields.append(CodingKeys.id.stringValue)}// Required
if let lName =try? lContainer.decode(String.self, forKey:.name){self.name = lName
}else{
lInvalidFields.append(CodingKeys.name.stringValue)}// Optional
if let lTwitter =try? lContainer.decode(Twitter.self, forKey:.twitter){self.profileUrl = lTwitter.profileUrl
self.createdAt = lTwitter.createdAt
}// Store object in CoreData when all required fields are valid// otherwise ignore record and generate an output on debug mode
if lInvalidFields.isEmpty {CDUser.setup(id:self.id, name:self.name, profileUrl:self.profileUrl, createdAt:self.createdAt)}else{#if DEBUG// lData.json format Data to a json string
if let lData =try?JSONEncoder().encode(self),let lJsonValue = lData.json(format: true, padding:""){print("\n\(type(of:self)): Invalid fields:\n [\(lInvalidFields.joined(separator:","))]\nValue:\n\(lJsonValue)\n")}#endif}}else{#if DEBUGprint("\(type(of:self)): Invalid data")#endif}}}
Class CoreData:
import Foundation
import CoreData
// "CD" for CoreData @objc(CDUser)openclassCDUser:NSManagedObject{@NSManagedpublicvarid:Int16@NSManagedpublicvarname:String@NSManagedpublicvarprofileUrl:String@NSManagedpublicvarcreatedAt:Datepublicstaticfunc setup(id pId:Int16,
name pName:String,
profileUrl pProfileUrl:String,
createdAt pCreatedAt:Date){// App.database is a CoreData layer abstraction object// Check if the object exist
guard let lInstance =App.database.objectFor(entity:"CDUser", id: pId)as?CDUserelse{// Object doesn't exists, create a new CoreData instance of CDUserletlNew=App.database.instance(CDUser.self)// Set properties of new CoreData object of CDUser
lNew.properties(id: pId, name: pName, profileUrl: pProfileUrl, price: pPrice, position: pPosition)return}// Object already exists, we only update his properties
lInstance.properties(id: pId, name: pName, profileUrl: pProfileUrl, createdAt: pCreatedAt)}publicfunc properties(id pId:Int16,
name pName:String,
profileUrl pProfileUrl:String,
createdAt pCreatedAt:Date){self.id = pId
self.name = pName
self.profileUrl = pProfileUrl
self.createdAt = pCreatedAt
// Save data in CoreDataApp.database.save()}}
The text was updated successfully, but these errors were encountered:
Can you generate something like that?
JSON:
Class Codable:
Class CoreData:
The text was updated successfully, but these errors were encountered: