[__NSCFArray objectForKey:]:从Core Data获取时发送到实例的无法识别的选择器

时间:2014-12-06 06:13:21

标签: ios core-data swift runtime-error

我在尝试从[__NSCFArray objectForKey:]: unrecognized selector sent to instance获取某些数据时收到错误Core Data

我对iOS编程比较陌生,并且不太了解这个错误试图告诉我的内容。我设置exception breakpoint来确定哪条线给我的问题,这就是这行代码:

if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [User] {

为什么我会收到此错误,如何解决?这与我保存Core Data的方式有关吗?我刚刚做了一个更改,我从服务器收到JSON,然后在后台处理,最后在主线程中保存对ManagedObjectContext的更改。

func getUser(userId: String) -> User? {
    let fetchRequest = NSFetchRequest(entityName: "User")
    fetchRequest.predicate = NSPredicate(format: "userId == %@", userId)
    fetchRequest.fetchLimit = 1

    if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [User] {
        if !fetchResults.isEmpty {
            return fetchResults[0]
        }
        else {
            println("UserId \(userId) not in database")
        }
    }
    return nil
}

错误

2014-12-06 13:09:36.087 LFDate[3162:51570] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7fb9e95c4310
2014-12-06 13:09:36.090 LFDate[3162:51570] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7fb9e95c4310'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001025c2f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010465ebb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001025ca04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010252227c ___forwarding___ + 988
    4   CoreFoundation                      0x0000000102521e18 _CF_forwarding_prep_0 + 120
    5   CoreFoundation                      0x000000010248beaf CFDictionaryGetValue + 159
    6   Foundation                          0x0000000102befd78 -[NSKeyedUnarchiver initForReadingWithData:] + 1698
    7   Foundation                          0x0000000102c135ea +[NSKeyedUnarchiver unarchiveObjectWithData:] + 66
    8   CoreData                            0x000000010210b4e0 _prepareResultsFromResultSet + 3552
    9   CoreData                            0x0000000102108930 newFetchedRowsForFetchPlan_MT + 3296
    10  CoreData                            0x00000001020f53ac -[NSSQLCore objectsForFetchRequest:inContext:] + 524
    11  CoreData                            0x00000001020f4e2b -[NSSQLCore executeRequest:withContext:error:] + 299
    12  CoreData                            0x00000001021ca4f3 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3331
    13  CoreData                            0x00000001021d27ee gutsOfBlockToNSPersistentStoreCoordinatorPerform + 190
    14  libdispatch.dylib                   0x00000001052117f4 _dispatch_client_callout + 8
    15  libdispatch.dylib                   0x00000001051f8774 _dispatch_barrier_sync_f_invoke + 365
    16  CoreData                            0x00000001021c53d5 _perform + 197
    17  CoreData                            0x00000001020f4ac8 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504
    18  CoreData                            0x00000001020f33e0 -[NSManagedObjectContext executeFetchRequest:error:] + 544
    19  LFDate                              0x0000000101b9c3b3 _TFC6LFDate14DatabaseHelper7getUserfS0_FSSGSqCS_4User_ + 1075
    20  LFDate                              0x0000000101c4ce01 _TFC6LFDate23MenuTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 4689
    21  LFDate                              0x0000000101c4e4ff _TToFC6LFDate23MenuTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
    22  UIKit                               0x000000010349d4b3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
    23  UIKit                               0x000000010347cfb1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
    24  UIKit                               0x0000000103492e3c -[UITableView layoutSubviews] + 213
    25  UIKit                               0x000000010341f973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
    26  QuartzCore                          0x000000010318ade8 -[CALayer layoutSublayers] + 150
    27  QuartzCore                          0x000000010317fa0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    28  QuartzCore                          0x000000010317f87e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    29  QuartzCore                          0x00000001030ed63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    30  QuartzCore                          0x00000001030ee74a _ZN2CA11Transaction6commitEv + 390
    31  QuartzCore                          0x00000001030eedb5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    32  CoreFoundation                      0x00000001024f7dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    33  CoreFoundation                      0x00000001024f7d20 __CFRunLoopDoObservers + 368
    34  CoreFoundation                      0x00000001024edb53 __CFRunLoopRun + 1123
    35  CoreFoundation                      0x00000001024ed486 CFRunLoopRunSpecific + 470
    36  GraphicsServices                    0x00000001072f09f0 GSEventRunModal + 161
    37  UIKit                               0x00000001033a6420 UIApplicationMain + 1282
    38  LFDate                              0x0000000101be33ae top_level_code + 78
    39  LFDate                              0x0000000101be33ea main + 42
    40  libdyld.dylib                       0x0000000105246145 start + 1
    41  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

0 个答案:

没有答案