在早期版本的swift中,我有一个核心数据函数
func retrieveItemsForRelatedEntity(entity: String, relatedEntity: String, identifier: String, sortDescriptors: [NSSortDescriptor]?) -> Array<AnyObject>? {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!
let frequest = NSFetchRequest(entityName: entity)
frequest.returnsObjectsAsFaults = false
if sortDescriptors != nil {
frequest.sortDescriptors = sortDescriptors
}
switch relatedEntity {
case "CostCentre":
frequest.predicate = NSPredicate(format: "costCentre.identifier == '\(identifier)'")
return try! context.executeFetchRequest(frequest)
case "CostCentreDay":
frequest.predicate = NSPredicate(format: "costCentreDay.identifier == '\(identifier)'")
return try! context.executeFetchRequest(frequest)
case "Resource":
frequest.predicate = NSPredicate(format: "resource.identifier == '\(identifier)'")
return try! context.executeFetchRequest(frequest)
case "ResourceDay":
frequest.predicate = NSPredicate(format: "resourceDay.identifier == '\(identifier)'")
return try! context.executeFetchRequest(frequest)
case "CostedDay":
frequest.predicate = NSPredicate(format: "costedDay.identifier == '\(identifier)'")
return try! context.executeFetchRequest(frequest)
default:
print("wrong entity for this function")
return nil
}
}
使用Swift 3.0,&#34;通用结果类型无法推断&#34;,所以我可以传入实体类型,然后为我的获取请求启用它,而不是传入实体标题字符串吗? / p>
答案 0 :(得分:2)
回答你的问题
所以不是传入实体标题字符串,我可以传入实体类型,并为我的获取请求启用它吗?
是的,那将是最好的方法。像
这样的东西 retrieveItemsForRelatedEntity(entity: NSManagedObject, relatedEntity: String, identifier: String, sortDescriptors: [NSSortDescriptor]?) -> Array<AnyObject>? {
您必须指定泛型类型,否则方法调用不明确。
第一个版本是为NSManagedObject定义的,第二个版本是使用扩展名为每个对象自动生成的,例如:
此扩展程序会自动生成(如下面的Animal
类型所示。
从Apple的文档中了解从Swift 2
到Swift 3
的更改
NSFetchRequest现在是基于新的NSFetchRequestResult协议的参数化类型。
executeFetchRequest(...)
的函数名在Swift 3中也发生了变化
public func fetch<T : NSFetchRequestResult>(_ request: NSFetchRequest<T>) throws -> [T]
Apple文档更改NSFetchRequest
Swift 2
func findAnimals() {
let request = NSFetchRequest(entityName:”Animal")
do {
guard let searchResults = try context.executeFetchRequest(request) as? [Animal] else {
print("Results were not of the expected structure")
}
... use(searchResults) ...
} catch {
print("Error ocurred during execution: \(error)")
}
}
Swift 3
func findAnimals() {
let request: NSFetchRequest<Animal> = Animal.fetchRequest
do {
let searchResults = try context.fetch(request)
... use(searchResults) ...
} catch {
print("Error with request: \(error)")
}
}
因此,在Swift 2中转换代码以创建NSFetchRequest
let frequest = NSFetchRequest(entityName: entity)
到Swift 3
let frequest: NSFetchRequest<YourEntityType> = YourEntityType.fetchRequest()
和
context.executeFetchRequest(frequest)
将是
context.fetch(frequest)
注意:通过网络上的其他示例,Animal.fetchRequest
的Apple文档代码示例可能有误,而应该是Animal.fetchRequest()
答案 1 :(得分:2)
请试试这个。它帮助了我。
func retrieveItemsForRelatedEntity(entity: String, relatedEntity: NSEntityDescription, identifier: String, sortDescriptors: [NSSortDescriptor]?) -> Array<AnyObject>? {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let frequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
和
switch relatedEntity {
case NSEntityDescription.entity(forEntityName: "CostCentre", in: context)!:
答案 2 :(得分:1)
试试这个:
let frequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity)