CoreData fetchRequest导致未捕获的异常

时间:2014-06-30 06:41:00

标签: core-data swift ios8

在CoreData中我想通过键获取元素。但是,执行获取请求会导致unrecognized selector sent to instance。我的代码摘录如下所示,最后一行失败。 managedObjectContext是一个有效的实例,因为我也是一个插入新实例的实例。这可能与Swift的Int64和CoreData的Integer 64有些不兼容吗?

var error : NSError?
var request = NSFetchRequest(entityName: "Node")
var nodeId = NSNumber(longLong: Int64(46763376))
request.predicate = NSPredicate(format: "id = %@", [nodeId])
var nodes = managedObjectContext.executeFetchRequest(request, error: &error)

数据模型中的实体定义:

Entity Definition

节点实体在Swift中定义为:

class Node : NSManagedObject {
    @NSManaged var id: NSNumber
    @NSManaged var lat: NSNumber
    @NSManaged var lon: NSNumber
    @NSManaged var tags: NSMutableSet
    @NSManaged var ways: NSMutableSet

    func description() -> String {
        return "Node(id=\(id), lat=\(lat), lon=\(lon))"
    }
}

堆栈追踪:

2014-06-30 08:27:58.238 SwiftApp[35316:1660108] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtCSs22ContiguousArrayStorage000000000B6E69C4 longLongValue]: unrecognized selector sent to instance 0xd7d0018'
*** First throw call stack:
(
    0   CoreFoundation                      0x00864646 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x0218f8e3 objc_exception_throw + 44
    2   CoreFoundation                      0x0086b575 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x007b6407 ___forwarding___ + 1047
    4   CoreFoundation                      0x007b5fce _CF_forwarding_prep_0 + 14
    5   CoreData                            0x0035a125 -[NSSQLiteConnection execute] + 1349
    6   CoreData                            0x0036e197 -[NSSQLChannel selectRowsWithStatement:] + 119
    7   CoreData                            0x0037767d newFetchedRowsForFetchPlan_MT + 1741
    8   CoreData                            0x00362723 -[NSSQLCore newRowsForFetchPlan:] + 403
    9   CoreData                            0x00361d85 -[NSSQLCore objectsForFetchRequest:inContext:] + 709
    10  CoreData                            0x00361740 -[NSSQLCore executeRequest:withContext:error:] + 448
    11  CoreData                            0x0045c6a5 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 2917
    12  CoreData                            0x00465be8 internalBlockToNSPersistentStoreCoordinatorPerform + 72
    13  libdispatch.dylib                   0x026a6488 _dispatch_client_callout + 14
    14  libdispatch.dylib                   0x0268ea38 _dispatch_barrier_sync_f_invoke + 124
    15  libdispatch.dylib                   0x0268e2c0 dispatch_barrier_sync_f + 89
    16  CoreData                            0x004574b3 _perform + 99
    17  CoreData                            0x00361345 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1525
    18  CoreData                            0x0035f575 -[NSManagedObjectContext executeFetchRequest:error:] + 597
    19  SwiftApp                            0x000189b6 _TFC8SwiftApp11AppDelegate6parserfS0_FTGSQCSo11NSXMLParser_15didStartElementGSQSS_12namespaceURIGSQSS_13qualifiedNameGSQSS_10attributesGSQCSo12NSDictionary__T_ + 18086
    20  SwiftApp                            0x0001a335 _TToFC8SwiftApp11AppDelegate6parserfS0_FTGSQCSo11NSXMLParser_15didStartElementGSQSS_12namespaceURIGSQSS_13qualifiedNameGSQSS_10attributesGSQCSo12NSDictionary__T_ + 1221
    21  Foundation                          0x00d1c00d _startElementNs + 3178
    22  libxml2.2.dylib                     0x02e8333d xmlParseStartTag2 + 4426
    23  libxml2.2.dylib                     0x02e860b2 xmlParseTryOrFinish + 3828
    24  libxml2.2.dylib                     0x02e84fc2 xmlParseChunk + 952
    25  Foundation                          0x00d19054 -[NSXMLParser parseData:] + 314
    26  Foundation                          0x00d194f5 -[NSXMLParser parseFromStream] + 276
    27  Foundation                          0x00d19639 -[NSXMLParser parse] + 33
    28  SwiftApp                            0x00012b72 _TFC8SwiftApp11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqCSo12NSDictionary__Sb + 1970
    29  SwiftApp                            0x000133a5 _TToFC8SwiftApp11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqCSo12NSDictionary__Sb + 101
    30  UIKit                               0x0105bb37 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 291
    31  UIKit                               0x0105c875 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2920
    32  UIKit                               0x0105fa33 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1507
    33  UIKit                               0x01077eb8 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
    34  UIKit                               0x0105e77e -[UIApplication workspaceDidEndTransaction:] + 29
    35  FrontBoardServices                  0x0381bf1f -[FBSWorkspace clientEndTransaction:] + 87
    36  FrontBoardServices                  0x038234ed __53-[FBSWorkspaceClient _queue_handleTransactionBookEnd]_block_invoke + 49
    37  CoreFoundation                      0x00788f90 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
    38  CoreFoundation                      0x0077e133 __CFRunLoopDoBlocks + 195
    39  CoreFoundation                      0x0077d898 __CFRunLoopRun + 936
    40  CoreFoundation                      0x0077d22b CFRunLoopRunSpecific + 443
    41  CoreFoundation                      0x0077d05b CFRunLoopRunInMode + 123
    42  UIKit                               0x0105e095 -[UIApplication _run] + 571
    43  UIKit                               0x010616e5 UIApplicationMain + 1526
    44  SwiftApp                            0x0001e471 top_level_code + 97
    45  SwiftApp                            0x0001eaeb main + 43
    46  libdyld.dylib                       0x026d0ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:2)

错误在于:

request.predicate = NSPredicate(format: "id = %@", [nodeId])

因为您将数组传递给predicateWithFormat的“变量参数列表”形式。 这两个版本都应该有效:

request.predicate = NSPredicate(format: "id = %@", nodeId)
request.predicate = NSPredicate(format: "id = %@", argumentArray: [nodeId])

备注: NSManagedObject文档声明您“强烈气馁” 从覆盖子类中的description方法。