Xcode 在核心数据NSFetchRequest中,获取NSInvalidArgumentException,将无法识别的选择器发送到实例

Xcode 在核心数据NSFetchRequest中,获取NSInvalidArgumentException,将无法识别的选择器发送到实例,xcode,core-data,Xcode,Core Data,代码如下(我通过print语句将其隔离,错误发生在这些行中): 核心数据通常不支持UUID字段排序。要使其正常工作,请将密钥路径更改为字符串类型,如下所示: [NSSortDescriptor(key: "id.description", ascending: false)] 键路径id.uuidString理论上会更好,但这仍然会产生与原始问题中描述的相同的崩溃(至少在macOS 11中)。出于兴趣,您是否使用SQLite备份存储?是的,根据编辑阅读错误消息,很清楚:您将

代码如下(我通过print语句将其隔离,错误发生在这些行中):


核心数据通常不支持UUID字段排序。要使其正常工作,请将密钥路径更改为字符串类型,如下所示:

[NSSortDescriptor(key: "id.description", ascending: false)]

键路径
id.uuidString
理论上会更好,但这仍然会产生与原始问题中描述的相同的崩溃(至少在macOS 11中)。

出于兴趣,您是否使用SQLite备份存储?是的,根据编辑阅读错误消息,很清楚:您将对定义为
nsuid
id
属性进行排序。但是,该类不可比较(不响应
比较:
)。@vadian谢谢!这有助于-通过切换到按字符串属性排序,错误消失了。我想这对于Swift新手来说是很难理解的,因为我希望编译器能够捕捉到类似这样的东西,但它无法推断,因为这不是在Swift内部发生的。非常乐意接受答案良好的解决方案,但描述可能会随时间/版本而变化。可能
id.uuistring
是一个更好的选择。@WarrenBurton我试过了,但是
uuistring
在macOS 11中崩溃了。
2020-12-31 08:01:35.180433-0700 Things[25892:958568] -[__NSConcreteUUID compare:]: unrecognized selector sent to instance 0x60000367a9e0
2020-12-31 08:01:35.190552-0700 Things[25892:958568] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSConcreteUUID compare:]: unrecognized selector sent to instance 0x60000367a9e0'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff20420af6 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff20177e78 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff2042f6f7 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
    3   CoreFoundation                      0x00007fff20425036 ___forwarding___ + 1489
    4   CoreFoundation                      0x00007fff20427068 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00007fff2084132b _NSCompareObject + 46
    6   CoreFoundation                      0x00007fff203aa09d __CFSimpleMergeSort + 74
    7   CoreFoundation                      0x00007fff203aa133 __CFSimpleMergeSort + 224
    8   CoreFoundation                      0x00007fff203a9c4a CFSortIndexes + 395
    9   CoreFoundation                      0x00007fff203aa5f6 CFMergeSortArray + 288
    10  Foundation                          0x00007fff208419d1 _sortedObjectsUsingDescriptors + 558
    11  Foundation                          0x00007fff20841bbe -[NSArray(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 232
    12  CoreData                            0x00007fff25113c7e -[NSManagedObjectContext executeFetchRequest:error:] + 4353
    13  libswiftCoreData.dylib              0x00007fff54a88993 $sSo22NSManagedObjectContextC8CoreDataE5fetchySayxGSo14NSFetchRequestCyxGKSo0gH6ResultRzlF + 51
    14  Things                              0x00000001048b7bdf $s6Things5GroupC6getAll7contextSayACGSo22NSManagedObjectContextC_tFZ + 591
    15  Things                              0x0000000104872da7 $s6Things16thingDataToThing2td7contextAA0E0CAA0eC0V_So22NSManagedObjectContextCtF + 1399
    16  Things                              0x00000001048836e5 $s6Things9APIServerC14syncWithRemote7context8callbackySo22NSManagedObjectContextC_yyctF14completionBothL_10groupDatas05thingN0ySayAA9GroupDataVG_SayAA05ThingQ0VGtF0O6MapperL_2tdAA0R0CAP_tF + 37
    17  Things                              0x0000000104883908 $s6Things9ThingDataVAA0B0Cs5Error_pIggozo_AcEsAF_pIegnrzo_TR + 56
    18  Things                              0x000000010488b034 $s6Things9ThingDataVAA0B0Cs5Error_pIggozo_AcEsAF_pIegnrzo_TRTA + 20
    19  libswiftCore.dylib                  0x00007fff2f8edfe2 $sSlsE3mapySayqd__Gqd__7ElementQzKXEKlF + 674
    20  Things                              0x000000010488313f $s6Things9APIServerC14syncWithRemote7context8callbackySo22NSManagedObjectContextC_yyctF14completionBothL_10groupDatas05thingN0ySayAA9GroupDataVG_SayAA05ThingQ0VGtF + 3855
    21  Things                              0x0000000104883b0c $s6Things9APIServerC14syncWithRemote7context8callbackySo22NSManagedObjectContextC_yyctF16completionGroupsL_10groupDatasySayAA9GroupDataVG_tF0kA0L_05thingN0ySayAA05ThingP0VG_tF + 108
    22  Things                              0x000000010488207f $sSay6Things9ThingDataVGIegg_ADIegn_TR + 15
    23  Things                              0x000000010487ecfc $s6Things9APIServerC7request3url15queryParameters6parser0C6Method10completion12errorHandler12httpBodyJsonySS_SDyS2SGx10Foundation4DataVcAA11RequestTypeOyxcySScAOSgtlFyAR_So13NSURLResponseCSgs5Error_pSgtcfU_yycfU_ + 348
    24  Things                              0x000000010488b463 $s6Things9APIServerC7request3url15queryParameters6parser0C6Method10completion12errorHandler12httpBodyJsonySS_SDyS2SGx10Foundation4DataVcAA11RequestTypeOyxcySScAOSgtlFyAR_So13NSURLResponseCSgs5Error_pSgtcfU_yycfU_TA + 51
    25  Things                              0x000000010487ed70 $sIeg_IeyB_TR + 48
    26  libdispatch.dylib                   0x0000000104c2d7ec _dispatch_call_block_and_release + 12
    27  libdispatch.dylib                   0x0000000104c2e9c8 _dispatch_client_callout + 8
    28  libdispatch.dylib                   0x0000000104c3ce75 _dispatch_main_queue_callback_4CF + 1152
    29  CoreFoundation                      0x00007fff2038edbb __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    30  CoreFoundation                      0x00007fff2038963e __CFRunLoopRun + 2685
    31  CoreFoundation                      0x00007fff203886d6 CFRunLoopRunSpecific + 567
    32  GraphicsServices                    0x00007fff2bededb3 GSEventRunModal + 139
    33  UIKitCore                           0x00007fff24690e0b -[UIApplication _run] + 912
    34  UIKitCore                           0x00007fff24695cbc UIApplicationMain + 101
    35  libswiftUIKit.dylib                 0x00007fff54d1e5f2 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
    36  Things                              0x000000010489497a $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 122
    37  Things                              0x00000001048948ee $s6Things11AppDelegateC5$mainyyFZ + 46
    38  Things                              0x00000001048949c9 main + 41
    39  libdyld.dylib                       0x00007fff202593e9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSConcreteUUID compare:]: unrecognized selector sent to instance 0x60000367a9e0'
terminating with uncaught exception of type NSException
CoreSimulator 732.18.6 - Device: iPhone 11 (93AB6812-3722-4022-B989-2E1423E6E6D0) - Runtime: iOS 14.3 (18C61) - DeviceType: iPhone 11
[NSSortDescriptor(key: "id.description", ascending: false)]