的Xcode 8帮忙,提供一个自动生成你的NSManagedObject
子extension
,它看起来像这样:findOrCreate斯威夫特卡伦特3没有硬编码的entityName
// Animal+CoreDataProperties.swift
import Foundation
import CoreData
extension Animal {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Animal> {
return NSFetchRequest<Animal>(entityName: "Animal");
}
// etc...
}
这是伟大的,因为我们不再有硬编码字符串,以创建NSFetchRequest
对象。然而,苹果公司并未提供协议来提及这种方法,也不提供entityName
本身。
例如,如果我想创建这样的方法:
protocol ManagedObjectType: class {}
extension ManagedObjectType where Self: NSManagedObject {
static func findOrCreate(inContext context: NSManagedObjectContext, matchingPredicate predicate: NSPredicate) -> Self {
// etc...
}
}
...有没有办法,我趁fetchRequest
方法的任何NSManagedObject
子类(因为它只是定义),所以我不得不再次硬编码这些字符串自己(即使Xcode已经这样做)。
确实有办法解决这个问题吗?我在想沿着这些路线的东西:
protocol ManagedObjectType: class {
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
}
extension Animal: ManagedObjectType {}
但是,这并不工作,因为协议不支持where
条款,所以我不能指定Self
是NSFetchRequestResult
,这意味着,当我来用它,像这样:
let request: NSFetchRequest<Self> = self.fetchRequest()
... Xcode中给了我这个错误:
'fetchRequest' produces 'NSFetchRequest<NSFetchRequestResult>', not the expected contextual result type 'NSFetchRequest<Self>'
有没有人解决了这个了吗?
这实际上并不奏效,因为我仍然无法在'T'上调用'fetchRequest'。不过,我的投票是因为你的答案帮助我达成了一个解决方案(见上文)。 – ganzogo
你可以使它扩展NSManagedObject,然后使用像 Animal.findOrCreate(...) 它会完全一样,你已经完成。纠正我,如果我错了 –
你是对的 - 你可以这样做,但它有点误导。 'findOrCreate'仍然会使用泛型来返回适当类型的东西;该方法在“动物”类上被调用的事实是无关紧要的。例如,这可以很好地工作:'让果实:Fruit = Animal.findOrCreate(...)'(尽管“Fruit”和“Animal”根本不相关)。 – ganzogo