gpt4 book ai didi

xcode - 检索实体名称时崩溃?

转载 作者:IT王子 更新时间:2023-10-29 06:32:11 25 4
gpt4 key购买 nike

当我的应用程序首次启动时,我会进行一些首次设置配置,即使用 CoreData API 将来自 JSON 文档的数据植入我的 SQLLite 数据库。

我通过首先从我的核心数据堆栈获取对我的托管对象上下文的引用,然后调用一个执行 JSON 配置的方法来执行此操作。然后我打电话:

....(JSON conversion)....

context.perform {
_ = Exercise.insert(into: context, name: exerciseName, category: categoryNumber, equipment: equipment, skill: "", primaryMuscle: primaryMuscle, otherMuscles: otherMuscles, info: exerciseDescription, image1: exerciseImage1, image2: exerciseImage2)
}

在我循环遍历 JSON 对象的每次迭代中,它会保留每个对象。

插入对象的方法是这样的:

public static func insert(into context: NSManagedObjectContext, name: String, category: Int16, equipment: String?, skill: String?, primaryMuscle: String?, otherMuscles: String?, info: String?, image1: String?, image2: String?) -> Exercise {
let exercise: Exercise = context.insertObject()
exercise.name = name
exercise.category = category
exercise.info = info
exercise.primaryMuscle = primaryMuscle
exercise.otherMuscles = otherMuscles
exercise.equipment = equipment
exercise.skill = skill

if image1 != nil {
exercise.image1Path = image1
}

if image2 != nil {
exercise.image2Path = image2
}

return exercise
}

然后我扩展了 NSManagedObjectContext...

extension NSManagedObjectContext {
public func insertObject<Object: NSManagedObject>() -> Object where Object: Managed {
guard let obj = NSEntityDescription.insertNewObject(forEntityName: Object.entityName, into: self) as? Object else { fatalError("Wrong object type") }
return obj
}
}

这是我的“托管”协议(protocol)声明:

public protocol Managed: class, NSFetchRequestResult {
static var entityName: String {get}
....
}

extension Managed where Self: NSManagedObject {
public static var entityName: String { return entity().name! }
}

然后当我尝试从我试图插入对象的协议(protocol)扩展中检索 entityName 时崩溃...

enter image description here

它给我错误“ fatal error :在展开可选值时意外发现 nil”

我没有生成我的 NSManagedObjectSublcasses,我自己创建了它们,但我确信我做对了。我有一种感觉,虽然当应用程序运行时,Core Data 没有选择我的模型类进行“练习”。这就是实体名称不返回任何内容的原因。所以这是我的模型类代码:

import UIKit
import CoreData

public class Exercise: NSManagedObject {

@NSManaged public fileprivate(set) var category: Int16
@NSManaged public fileprivate(set) var image1Path: String?
@NSManaged public fileprivate(set) var image2Path: String?
@NSManaged public fileprivate(set) var equipment: String?
@NSManaged public fileprivate(set) var info: String?
@NSManaged public fileprivate(set) var skill: String?
@NSManaged public fileprivate(set) var primaryMuscle: String?
@NSManaged public fileprivate(set) var otherMuscles: String?

public static let normalizedNameKey = "name_normalized"

@NSManaged fileprivate var primitiveName: String

public var name: String {
set {
willChangeValue(forKey: #keyPath(Exercise.name))
primitiveName = newValue
updateNormalizedName(newValue)
didChangeValue(forKey: #keyPath(Exercise.name))
}
get {
willAccessValue(forKey: #keyPath(Exercise.name))
let value = primitiveName
didAccessValue(forKey: #keyPath(Exercise.name))
return value
}
}

fileprivate func updateNormalizedName(_ name: String) {
setValue(name.normalizedForSearch, forKey: Exercise.normalizedNameKey)
}

public var exerciseImage1: UIImage? {
guard let image = image1Path else {return nil}
return UIImage(named: image)
}

public var exerciseImage2: UIImage? {
guard let image = image2Path else {return nil}
return UIImage(named: image)
}

public static func insert(into context: NSManagedObjectContext, name: String, category: Int16, equipment: String?, skill: String?, primaryMuscle: String?, otherMuscles: String?, info: String?, image1: String?, image2: String?) -> Exercise {
let exercise: Exercise = context.insertObject()
exercise.name = name
exercise.category = category
exercise.info = info
exercise.primaryMuscle = primaryMuscle
exercise.otherMuscles = otherMuscles
exercise.equipment = equipment
exercise.skill = skill

if image1 != nil {
exercise.image1Path = image1
}

if image2 != nil {
exercise.image2Path = image2
}

return exercise
}

}

extension Exercise: Managed {

public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(key: #keyPath(name), ascending: false)]
}

public static func defaultPredicate(muscleCategory: Int) -> NSPredicate
{
return NSPredicate(format: "%K == %ld",
#keyPath(Exercise.category), muscleCategory)
}
}

这是数据模型编辑器中的实体: enter image description here

有什么可能出错的想法吗?谢谢

最佳答案

我有完全相同的问题,使用完全相同的代码(Florian Kugler 的核心数据书)。

我的问题出现在我添加了一个新的实体+关系之后,并且在创建新实体时,实体名称为零。
我通过删除实体并再次重新创建来修复它。错误消失了。

关于xcode - 检索实体名称时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44582907/

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