ios - 向 NSManagedObject 类添加函数

标签 ios swift nsmanagedobject

我有一个从 Xcode 为我的尺寸模型生成的 Size 类,如下:

@objc(Size)
public class Size: NSManagedObject {
    override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?) {
        super.init(entity: entity, insertInto: context)
    }
}

extension Size {

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

    @NSManaged public var id: Int16
    @NSManaged public var name: String?

    var quantity: Int

    func setQuantity() {}  
}

Quantity var 和 setQuantity 方法无法从我的 ViewController 访问,尽管我在声明之前将其公开,有什么建议吗?

最佳答案

您将收到编译器错误:扩展不得包含存储的属性

您应该更新它并将变量放入您的类而不是扩展中。

public class Size: NSManagedObject {
     var quantity: Int
     // Same for other variables.
     // Your existing code goes here.
}

尝试并分享您的结果。

关于ios - 向 NSManagedObject 类添加函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53476307/

相关文章:

iOS Swift - UITableView 的最佳方法

ios - 在 Swift 中对从核心数据接收到的数组进行排序

swift - 有没有办法在 Core Data 中使用带有参数的计算属性?

iphone - Xcode 4 : Replace NSManagedObject subclass

ios - Swift - 获取从今天开始的最后 7 天数组

ios - 文件路径为零

objective-c - #pragma clang 诊断的 Swift 替代方案

ios - 如果我的应用程序会记住我购买了应用程序内购买,我该怎么办

iphone - 检测在 calloutAccessoryControlTapped 委托(delegate)中点击了哪个附件 View

ios - 未找到 UIViewController 上的 Xamarin.iOS/MonoTouch Storyboard Segue