使用对象作为 CoreData 对象字段类型

Using objects as CoreData object field type

是否可以将 CoreData 对象字段类型配置为对象?例如“CustomTimeObject”:

import Foundation
import CoreData


extension someObject{

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

    @NSManaged public var id: UUID
    @NSManaged public var startTime: CustomTimeObject


}

我的意图是在调用 Core Data 时使用谓词,以便检索具有匹配的“startTime”特定 CustomTimeObject 字段的对象,例如(即使不工作):

            var customTime = [CustomTimeObject]
            guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
              

            let managedContext = appDelegate.persistentContainer.viewContext
              
     
    
            let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "SomeObject")


            fetchRequest.predicate = NSPredicate(format: "startTime.hour > %@ AND startTime.minutes < %@", argumentArray: [8, 11])


              do
              {
                  let results = try managedContext.fetch(fetchRequest)
              
                
                for result in results as! [NSManagedObject] {
                        var timeObject = CustomTimeObject()
                        timeObject.hour=result.value(forKey: "startTime.hour")
                        timeObject.minutes=result.value(forKey: "startTime.minutes")
                        customTime.append(timeObject)
                }
                
              
     
                 
              }
                
              catch
              {
                  print(error)
              }

您应该将 CustomTimeObject 作为另一个实体,并将其作为第一个 someObject 中的关系。查看 Apple 的 link:Creating Managed Object Relationships