gpt4 book ai didi

ios - 我收到 1570 Cocoa 错误,因为 var 与父实体的关系导致 nil Core Data swift

转载 作者:行者123 更新时间:2023-11-29 05:52:20 26 4
gpt4 key购买 nike

我正在尝试将新记录保存到 CoreData,但当我尝试时,出现 1570 Cocoa 错误。在错误中,我看到的唯一的 nil 是 user ,它是 user var 与父实体的关系,以及在属性检查器中设置为可选的其他两个属性,所以他们不是必需的。我不明白这一点,因为我确实执行了用户获取并检查事件登录是否与获取的用户名相同,并且该值是否正确......也许有一些我没有从错误打印中看到的内容。

这是函数:

static func saveBooking(bookingId: String, bookingDate: String, bookingStart: String, bookingEnd: String, customerName: String, price: String, workList: String) throws {
let context = CoreData.databaseContext

let request: NSFetchRequest<User> = User.fetchRequest()
do {
let fetch = try context.fetch(request)
print("@@@@@@@@@@@@@@@@@@ fetching user")
for value in fetch {
if value.name == UserDetails.fullName {
print("User is: \(value.name!)") //correct

if #available(iOS 10.0, *) {
let booking = Booking(context: context)
booking.user?.name = value.name! // results nil and causes error when saving
booking.bookingId = bookingId
booking.bookingDate = bookingDate
booking.bookingStart = bookingStart
booking.bookingEnd = bookingEnd
booking.customerName = customerName
booking.bookingPrice = price
booking.worksList = workList

do {
try context.save()
print("@@@@@@@@@@@@ New booking is saved")
} catch {
print("@@@@@@@@@@@@@@ Error saving new booking")
print(error)
}


} else {
// Fallback on earlier versions
let entityDescription = NSEntityDescription.entity(forEntityName: "Booking", in: context)
let booking = Booking(entity: entityDescription!, insertInto: context)
booking.user?.name = value.name
booking.bookingId = bookingId
booking.bookingDate = bookingDate
booking.bookingStart = bookingStart
booking.bookingEnd = bookingEnd
booking.customerName = customerName
booking.bookingPrice = price
booking.worksList = workList
do {
try context.save()
print("@@@@@@@@@@@@ New booking is saved")
} catch {
print("@@@@@@@@@@@@@@ Error saving new booking")
print(error)
}
}
}
}
} catch {
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Error in fetching a user")
}
}

这是错误的打印结果:

@@@@@@@@@@@@@@    Error saving new booking
Error Domain=NSCocoaErrorDomain Code=1560 "(null)" UserInfo={NSDetailedErrors=(
"Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo={NSValidationErrorObject=<Booking: 0x6000006240f0> (entity: Booking; id: 0x600002501860 <x-coredata:///Booking/t0A133AFE-B419-4941-AADC-6C9AE234A8EF2> ; data: {\n bookingDate = 20190405;\n bookingEnd = \"15:30\";\n bookingId = 201904051430;\n bookingPrice = \"45.0\";\n bookingStart = \"14:30\";\n customerName = \"vincenzo calia\";\n shopLogoUrl = nil;\n shopName = nil;\n user = nil;\n worksList = \"Revisione Generale, \";\n}), NSValidationErrorKey=user, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.)}",
"Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo={NSValidationErrorObject=<Booking: 0x600000630190> (entity: Booking; id: 0x60000251b120 <x-coredata:///Booking/t0A133AFE-B419-4941-AADC-6C9AE234A8EF3> ; data: {\n bookingDate = 20190405;\n bookingEnd = \"18:30\";\n bookingId = 201904051730;\n bookingPrice = \"45.0\";\n bookingStart = \"17:30\";\n customerName = \"vincenzo calia\";\n shopLogoUrl = nil;\n shopName = nil;\n user = nil;\n worksList = \"Revisione Generale, \";\n}), NSValidationErrorKey=user, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.)}"
)}

你能看出问题出在哪里吗?像往常一样非常感谢

最佳答案

在保存上下文之前,我缺少将新条目添加到实体的父级,并且显然引发了错误,因此我只添加了行 value.addToBookings(booking) ,一切都按预期工作。实际上,我可以完全避免为父关系赋予值,因为我实际上直接通过父函数添加。希望这对其他人有帮助。

关于ios - 我收到 1570 Cocoa 错误,因为 var 与父实体的关系导致 nil Core Data swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55549914/

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