ios – NSKeyedUnarchiver error in Swift Core Knowledge


I’ve this core knowledge concern:
enter image description here

*** -[NSKeyedUnarchiver decodeObjectForKey:]: worth for key (id) isn't an object. It will turn out to be an error sooner or later.

First I wish to describe what I’m attempting to do:
I carry out a GET request to my API, get an array of parts and add the info I bought to Core Knowledge.

Right here is my CoreData Entity:

extension PetEntity {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<PetEntity> {
        return NSFetchRequest<PetEntity>(entityName: "PetEntity")
    }

    @NSManaged public var class: CategoryDTO?
    @NSManaged public var id: Int64
    @NSManaged public var identify: String?
    @NSManaged public var photoUrls: [String]?
    @NSManaged public var standing: String?
    @NSManaged public var tags: [TagDTO]?

}

extension PetEntity : Identifiable {

}

My DTO I take advantage of to get Json knowledge in after which map it to my mannequin:

struct PetDTO: Codable {
    var id: Int
    var class: CategoryDTO?
    var identify: String?
    var photoUrls: [String]?
    var tags: [TagDTO]?
    var standing: StatusDTO
}

public class CategoryDTO: NSObject, Codable {
    var id: Int
    var identify: String?
    
    public enum CodingKeys: String, CodingKey {
        case id
        case identify
    }
    
    public required init?(coder: NSCoder) {
        id = coder.decodeObject(forKey: CodingKeys.id.rawValue) as? Int ?? 0 // error right here
        identify = coder.decodeObject(forKey: CodingKeys.identify.rawValue) as? String
    }
}

extension CategoryDTO: NSSecureCoding{
    public static var supportsSecureCoding: Bool{
        return true
    }
    public func encode(with coder: NSCoder) {
        coder.encode(id, forKey: CodingKeys.id.rawValue)
        coder.encode(identify, forKey: CodingKeys.identify.rawValue)
    }
}

@objc(CategoryDtoTransformer)
public ultimate class CategoryDtoTransformer: NSSecureUnarchiveFromDataTransformer {
    public static let identify = NSValueTransformerName(rawValue: String(describing: CategoryDtoTransformer.self))
    public override static var allowedTopLevelClasses: [AnyClass] {
        return [CategoryDTO.self, NSString.self, NSArray.self]
    }

    @objc dynamic
    public static func register() {

        let transformer = CategoryDtoTransformer()
        ValueTransformer.setValueTransformer(transformer, forName: identify)
    }
}

public class TagDTO: NSObject, Codable {
    var id: Int
    var identify: String?
    
    public enum CodingKeys: String, CodingKey {
        case id
        case identify
    }
    
    public required init?(coder: NSCoder) {
        id = coder.decodeObject(forKey: CodingKeys.id.rawValue) as? Int ?? 0 // Error right here
        identify = coder.decodeObject(forKey: CodingKeys.identify.rawValue) as? String
    }
}

extension TagDTO: NSSecureCoding{
    public static var supportsSecureCoding: Bool{
        return true
    }
    public func encode(with coder: NSCoder) {
        coder.encode(id, forKey: CodingKeys.id.rawValue)
        coder.encode(identify, forKey: CodingKeys.identify.rawValue)
    }
}

@objc(TagDtoTransformer)
public ultimate class TagDtoTransformer: NSSecureUnarchiveFromDataTransformer {
    public static let identify = NSValueTransformerName(rawValue: String(describing: TagDtoTransformer.self))
    public override static var allowedTopLevelClasses: [AnyClass] {
        return [TagDTO.self, NSString.self, NSArray.self]
    }

    @objc dynamic
    public static func register() {

        let transformer = TagDtoTransformer()
        ValueTransformer.setValueTransformer(transformer, forName: identify)
    }
}

enum StatusDTO: String, Codable {
    case accessible
    case bought
    case pending
}

And right here is my CRUD code for including to database:

class CoreDataPetPersistance: PetPerstitenceProtocol {
    
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    func add(pet: PetDTO) {
        let newPet = PetEntity(context: self.context)
        newPet.id = Int64(pet.id)
        newPet.identify = pet.identify
        newPet.class = pet.class
        newPet.photoUrls = pet.photoUrls
        newPet.tags = pet.tags
        newPet.standing = pet.standing.rawValue
        
        do {
            strive self.context.save()
        } catch {
            print(error)
        }
    }
 }

Additionally I’ve added constraints to my ID to make every pet I get from API distinctive and registered CategoryDTO and TagDTO in app delegate. It needs to be effective.

Full error I get:

CoreData: error: -executeRequest: encountered exception = Error Area=NSCocoaErrorDomain Code=133000 "Try and entry an object not present in retailer." UserInfo={objectID=0xa57351074a65fcb3 <x-coredata://FEB02FEE-9B8B-4785-82A1-263F82D4CDBC/PetEntity/p1095>} with userInfo = {
    NSCocoaErrorDomain = 133000;
    NSUnderlyingError = "Error Area=NSCocoaErrorDomain Code=133000 "Try and entry an object not present in retailer." UserInfo={objectID=0xa57351074a65fcb3 <x-coredata://FEB02FEE-9B8B-4785-82A1-263F82D4CDBC/PetEntity/p1095>}";
}

Please assist me repair this and clarify why this error occurs

Thanks

Similar Posts

Leave a Reply

Your email address will not be published.