本文介绍了Swift和CoreData以自定义类作为可转换对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有swift和CoreData的自定义类作为可转换对象.我一直在浪费时间试图弄清楚,但不能.我不断收到两个错误:无法将属性标记为@NSManaged,因为它的类型无法在Objective-C中表示,并且无法将属性声明为公共,因为它的类型使用内部类型.
非常感谢您的帮助.谢谢.

I am trying to use a custom class with swift and CoreData as a transformable object. I have been wasting hours trying to figure it out but cannot. I keep getting two errors: Property cannot be marked @NSManaged because its type cannot be represented in Objective-C and Property cannot be declared public because its type uses an internal type.
Help is greatly appreciated. Thanks.

我有一个名为User的CoreData对象.我还有一个名为Site的对象.
我不希望Site对象成为CoreData对象,因为大多数情况下,我不希望将Site对象添加到CoreData上下文中.

I have a CoreData object named User. I also have an object named Site.
I don't want the Site object to be a CoreData object because the majority of the times that I create a Site object I don't want it added to the CoreData context.

在下面,您将看到"Site"类型的"primarySite"和"Site"类型的数组"sites".这两行给出了我上面列出的相同的两个错误.

Below you will see "primarySite" of type "Site" and "sites" which is an array of type "Site". These two lines give the same two errors I listed above.

数据模型

用户+ CoreDataClass

import Foundation
import CoreData

public class User: NSManagedObject {
}

User + CoreDataProperties

import Foundation
import CoreData

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

    @NSManaged public var firstName: String?
    @NSManaged public var lastName: String?
    @NSManaged public var role: String?
    @NSManaged public var primarySite: Site
    @NSManaged public var sites: [Site]
}

站点

import UIKit

class Site: NSCoding {
    let identifier: Int32
    let name: String
    let note: String

    init(with values: Dictionary<String,Any>) {
        identifier = Int32(values["identifier"] as! String)!
        name = values["name"] as! String
        note = values["note"] as! String
    }

    required convenience init(coder decoder: NSCoder) {
        var siteValues = [String:Any]()
        siteValues["identifier"] = decoder.decodeObject(forKey: "identifier") as! String
        siteValues["name"] = decoder.decodeObject(forKey: "name") as! String
        siteValues["note"] = decoder.decodeObject(forKey: "note") as!
        self.init(with: siteValues)
    }

     func encode(with coder: NSCoder) {
        coder.encode(String(self.identifier), forKey: "identifier")
        coder.encode(self.name, forKey: "name")
        coder.encode(self.note, forKey: "note")
    }
}

推荐答案

我弄清楚了如何解决这两个错误.

I figured out how to resolve both errors.

1)无法将属性标记为@NSManaged,因为其类型无法在Objective-C中表示"
    由于@Tom的回答,我通过使Site成为NSObject的子类来解决此错误.

1) "Property cannot be marked @NSManaged because its type cannot be represented in Objective-C"
     Thanks to @Tom's answer I resolve this error by making Site a subclass of NSObject.

class Site: NSObject, NSCoding {


2)由于属性类型使用内部类型,因此无法将其声明为公共属性"
我通过在类,编码器和解码器功能之前添加"public"来解决此错误.


2) "Property cannot be declared public because its type uses an internal type"
     I resolve this error by making adding "public" before the class, and the encoder and decoder functions.

public class Site: NSObject, NSCoding {
...
public func encode(with coder: NSCoder) {...}
public required convenience init(coder decoder: NSCoder) {...}

这篇关于Swift和CoreData以自定义类作为可转换对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:08