首先,我想指出,有许多问题报告了类似的错误,但我的情况不同。
我有三个核心数据实体:
训练,它有一个功能集对象的集合
FeatureSet,它包含FeatureGroup对象的集合
FeatureGroup,它只有数字字段
每个实体都有其逆关系(例如,FeatureSet与训练有逆多对一关系)。
ios - 核心数据:无法识别的选择器已发送到实例-LMLPHP
helper类的这三种方法执行将对象插入托管对象上下文的操作:

// MARK: - Insertion
func insertFeatureGroup(_ groupDict:[String:Any], inSet set:FeatureSet, forData data:String) -> FeatureGroup? {
    guard let group = NSEntityDescription.insertNewObject(forEntityName: "FeatureGroup", into: self.context) as? FeatureGroup else { return nil }

    group.data = data
    group.set = set
    if let min = groupDict["min"] as? Double { group.min = min as NSNumber }
    if let max = groupDict["max"] as? Double { group.max = max as NSNumber }
    if let mean = groupDict["mean"] as? Double { group.mean = mean as NSNumber }
    if let median = groupDict["median"] as? Double { group.median = median as NSNumber }
    if let deviation = groupDict["deviation"] as? Double { group.deviation = deviation as NSNumber }
    if let skewness = groupDict["skewness"] as? Double { group.skewness = skewness as NSNumber }
    if let kurtosis = groupDict["kurtosis"] as? Double { group.kurtosis = kurtosis as NSNumber }
    return group
}

func insertFeatureSet(_ featuresDict:[String:Any], inTraining training:Training) -> FeatureSet? {
    guard let set = NSEntityDescription.insertNewObject(forEntityName: "FeatureSet", into: self.context) as? FeatureSet else { return nil }
    guard let sequenceNumber = featuresDict["sequenceNumber"] as? Int16 else { return nil }

    training.addToSets(set)
    set.training = training
    set.sequenceNumber = sequenceNumber

    if let heartRate = featuresDict["heartRate"] as? [String:Any] {
        if let group = insertFeatureGroup(heartRate, inSet: set, forData: "heartRate") {
            set.addToGroups(group)
        }
    }

    if let rotationRate = featuresDict["rotationRate"] as? [String:Any] {
        if let group = insertFeatureGroup(rotationRate, inSet: set, forData: "rotationRate") {
            set.addToGroups(group)
        }
    }

    if let userAcceleration = featuresDict["userAcceleration"] as? [String:Any] {
        if let group = insertFeatureGroup(userAcceleration, inSet: set, forData: "userAcceleration") {
            set.addToGroups(group)
        }
    }

    if let altitude = featuresDict["altitude"] as? [String:Any] {
        if let group = insertFeatureGroup(altitude, inSet: set, forData: "altitude") {
            set.addToGroups(group)
        }
    }

    if let course = featuresDict["course"] as? [String:Any] {
        if let group = insertFeatureGroup(course, inSet: set, forData: "course") {
            set.addToGroups(group)
        }
    }

    if let speed = featuresDict["speed"] as? [String:Any] {
        if let group = insertFeatureGroup(speed, inSet: set, forData: "speed") {
            set.addToGroups(group)
        }
    }

    return set
}

func insertTraining(activity:String) -> Training? {
    guard let training = NSEntityDescription.insertNewObject(forEntityName: "Training", into: self.context) as? Training else { return nil }
    training.activity = activity
    return training
}

这就是我执行提取的方式:
// MARK: - Fetch
var trainings:[Training]? {
    let request = NSFetchRequest<NSFetchRequestResult>()
    request.returnsObjectsAsFaults = false
    let entity = NSEntityDescription.entity(forEntityName: "Training", in: self.context)
    request.entity = entity

    let sortDesc = NSSortDescriptor(key: "startTime", ascending: true)
    request.sortDescriptors = [sortDesc]

    do {
        guard let trainings = try self.context.fetch(request) as? [Training] else {
            return nil
        }
        return trainings
    } catch {
        print("Error Fetching Trainings: \(error)")
        return nil
    }
}

当我试图访问featureSet对象的groups属性时,问题就来了。让我们支持在我的代码中的某些部分,我获取所有的培训(它们都成功获取):
guard let trainings = <myUtilityClassInstance>.trainings else { return }
guard let myTraining = trainings.first else { return }
guard let mySet = myTraining.sets.first as? FeatureSet else { return }
print(mySet.groups)

最后一行代码产生异常:“发送到实例0x1700994b0的未识别选择器”。如果我尝试检查对象mySet的类,我可以看到它实际上是一个FeatureSet对象。这个类有一个“groups”属性。然而,似乎无法识别groups属性,访问它会产生异常。
我的Xcode项目很大,我意识到问题可能出在代码的其他部分。我在找一个关于如何调查这个异常的建议。
备注:我使用的是由核心数据自动生成的类(Training、FeatureSet、FeatureGroup),其中包括一些属性访问器(例如,在FeatureSet中添加/删除组的方法)。
更新:FeatureSet类定义为空,但这些是由CoreData自动生成的扩展:
extension FeatureSet {

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

    @NSManaged public var sequenceNumber: Int16
    @NSManaged public var groups: NSSet?
    @NSManaged public var training: Training?

}

// MARK: Generated accessors for groups
extension FeatureSet {

    @objc(addGroupsObject:)
    @NSManaged public func addToGroups(_ value: FeatureGroup)

    @objc(removeGroupsObject:)
    @NSManaged public func removeFromGroups(_ value: FeatureGroup)

    @objc(addGroups:)
    @NSManaged public func addToGroups(_ values: NSSet)

    @objc(removeGroups:)
    @NSManaged public func removeFromGroups(_ values: NSSet)

}

最佳答案

结果发现,当我试图访问FeatureSet的groups属性时,用于获取数据的NSManagedObjectContext对象被释放。关于这件事的更多信息可以找到here

关于ios - 核心数据:无法识别的选择器已发送到实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46353961/

10-08 23:10