[标签比较:]:发送到实例的无法识别的选择器
[Label compare:]: unrecognized selector sent to instance
我的应用程序因错误 unrecognized selector sent to instance
而崩溃,只是在我的应用程序 swift 文件中突出显示 @main
,这并没有给我任何可能导致该错误的线索错误。
我认为这可能与@FetchRequest 中的数据冲突有关。我已将错误隔离到这些代码片段:
struct SidebarView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(entity: Label.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Label.name, ascending: true)])
var labels: FetchedResults<Label>
var body: some View {
let allLabelNames = labels.map { "\([=10=].value(forKeyPath: "name")!)" }
let labelNames = allLabelNames.uniqued()
VStack{
NavigationLink(destination: AllDataView(names: labelNames)){
Text("View all data")
}
}
}
struct AllDataView: View {
var imagesFetchRequest: FetchRequest<Sample>
var images: FetchedResults<Sample>{
imagesFetchRequest.wrappedValue
}
private var names: [String]
init(names: [String]){
let samplesSortDescriptor = NSSortDescriptor(keyPath: \Sample.label, ascending: true)
let predicate = NSPredicate(format: "label.name IN %@", names)
self.names = names
imagesFetchRequest = FetchRequest<Sample>(entity: Sample.entity(), sortDescriptors: [samplesSortDescriptor], predicate: predicate)
}
}
注意 Label 是 NSManagedObject
关于如何调试此问题以在我的应用程序中找到导致它崩溃的确切行的任何想法?
NSSortDescriptor(keyPath: \Sample.label, ascending: true)
=>
NSSortDescriptor(keyPath: \Sample.label.name, ascending: true)
在第一个中,您试图在 label
(Label
) 属性.
上对 Sample
进行排序
所以在某个时候,它会做 [someLabelInstance compare:someOtherLabelInstance]
来知道是否需要将哪个放在另一个之前。但是,Label
没有实例方法 compare()
,这就是你得到错误的原因。
实际上,您想要对 label
的 name
属性 进行排序。 属性 这是一个 String
(通过阅读您的代码隐含),并且是“可排序的”(实现 compare()
)。
我的应用程序因错误 unrecognized selector sent to instance
而崩溃,只是在我的应用程序 swift 文件中突出显示 @main
,这并没有给我任何可能导致该错误的线索错误。
我认为这可能与@FetchRequest 中的数据冲突有关。我已将错误隔离到这些代码片段:
struct SidebarView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(entity: Label.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Label.name, ascending: true)])
var labels: FetchedResults<Label>
var body: some View {
let allLabelNames = labels.map { "\([=10=].value(forKeyPath: "name")!)" }
let labelNames = allLabelNames.uniqued()
VStack{
NavigationLink(destination: AllDataView(names: labelNames)){
Text("View all data")
}
}
}
struct AllDataView: View {
var imagesFetchRequest: FetchRequest<Sample>
var images: FetchedResults<Sample>{
imagesFetchRequest.wrappedValue
}
private var names: [String]
init(names: [String]){
let samplesSortDescriptor = NSSortDescriptor(keyPath: \Sample.label, ascending: true)
let predicate = NSPredicate(format: "label.name IN %@", names)
self.names = names
imagesFetchRequest = FetchRequest<Sample>(entity: Sample.entity(), sortDescriptors: [samplesSortDescriptor], predicate: predicate)
}
}
注意 Label 是 NSManagedObject
关于如何调试此问题以在我的应用程序中找到导致它崩溃的确切行的任何想法?
NSSortDescriptor(keyPath: \Sample.label, ascending: true)
=>
NSSortDescriptor(keyPath: \Sample.label.name, ascending: true)
在第一个中,您试图在 label
(Label
) 属性.
上对 Sample
进行排序
所以在某个时候,它会做 [someLabelInstance compare:someOtherLabelInstance]
来知道是否需要将哪个放在另一个之前。但是,Label
没有实例方法 compare()
,这就是你得到错误的原因。
实际上,您想要对 label
的 name
属性 进行排序。 属性 这是一个 String
(通过阅读您的代码隐含),并且是“可排序的”(实现 compare()
)。