核心数据:发送到实例的无法识别的选择器
Posted
技术标签:
【中文标题】核心数据:发送到实例的无法识别的选择器【英文标题】:Core Data: unrecognized selector sent to instance 【发布时间】:2017-09-21 22:16:05 【问题描述】:首先我想指出,有很多问题报告了类似的错误,但我的情况不同。
我有这三个核心数据实体:
-
训练,包含一组 FeatureSet 对象
FeatureSet,包含一组 FeatureGroup 对象
FeatureGroup,只有数字字段
每个实体都有其反向关系(例如,FeatureSet 与 Training 具有反向多对一关系)。
帮助类的这三个方法将对象插入到托管对象上下文中:
// 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 属性并且访问它会产生异常。 我的 Xcode 项目很大,我意识到问题可能出在代码的其他部分。我正在寻找有关如何调查此异常的建议。
PS:我正在使用由 Core Data 自动生成的类(Training、FeatureSet、FeatureGroup),其中包括一些属性访问器(例如,在特征集)。
更新: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 类,尤其是定义组的地方? @clarus 我添加了代码。 错误通常会说明是哪个类和哪个选择器导致了问题。你有吗? (我知道你假设FeatureSet.groups
但它实际报告的内容是这样的吗?)
@PhillipMills "[My_Target.FeatureSet groups]"
我想知道这是否真的是因为 print() 本身。如果将 mySet.groups 放在变量中而不是打印它,它还会崩溃吗?我认为 NSSet?
在 Swift 中将被视为 Set<AnyObject>?
,并且您可能必须遍历元素以打印它们,或者将 NSSet?
转换为其他内容才能直接打印它。跨度>
【参考方案1】:
事实证明,当我试图访问 FeatureSet 的 groups 属性时,用于获取数据的 NSManagedObjectContext 对象被释放.有关此事的更多信息可以找到here。
【讨论】:
以上是关于核心数据:发送到实例的无法识别的选择器的主要内容,如果未能解决你的问题,请参考以下文章
核心数据错误 - NSDatelocalizedCaseInsensitiveCompare:无法识别的选择器发送到实例
核心数据-[Decodable.Address initWithCoder:]:发送到实例的无法识别的选择器