gpt4 book ai didi

ios - 将对象用作核心数据对象字段类型

转载 作者:行者123 更新时间:2023-12-01 16:11:45 25 4
gpt4 key购买 nike

是否可以将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的以下链接:Creating Managed Object Relationships

关于ios - 将对象用作核心数据对象字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62778903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com