我的数据模型中有两个实体,如下图所示:
Product
Attendance
我正在保存与出席关系良好的产品!现在,我需要带着一个特定的出席者去拿产品
我正在尝试以下代码:

func fetchProducts() {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    let fetchRequest = NSFetchRequest(entityName: "Product")
    let attendancePredicate = NSPredicate(format: "attendance.objectID == \(currAttendance.objectID)")
    fetchRequest.predicate = attendancePredicate

    print("Att = \(currAttendance.name!)")
    viewTypeSKU.hidden = true

    do {
        let results = try managedContext.executeFetchRequest(fetchRequest)
        products = results as! [NSManagedObject]
    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }

}

我得到错误:'Unable to parse the format string "attendance.objectID ==

最佳答案

在fetchSELF中可以与NSManagedObjectIDNSManagedObject进行比较。但是,不能对要获取的项执行.objectID。我的理论是,这是因为在执行.时,它会为.objectIDNSManagedObject一部分的特定实体寻找属性和/或关系。但我可能错了。
但是,如果您只是将NSPredicate切换到其中一个,它将工作。
NSPredicate(format: "attendance == %@", currAttendance.objectID)

NSPredicate(format: "attendance == %@", currAttendance)

关于swift - 使用objectID获取核心数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37331281/

10-09 21:08
查看更多