core-data - CoreData fetchRequest 导致未捕获的异常

标签 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

最佳答案

错误在这里:

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

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

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

备注: NSManagedObject 文档声明您“强烈反对” 从重写子类中的 description 方法。

关于core-data - CoreData fetchRequest 导致未捕获的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24484263/

相关文章:

swift - 覆盖 init(coder aDecoder : NSCoder!) 没有像预期的那样工作 - Swift

ios - UIButton 不改变图像?

ios - 接收自定义通知 iOS swift 2.0

ios - 在 swift 2.1 中使用 MapKit 添加不同的图钉颜色

ios - 是否可以在纯 Objective-C 中编写 iOS 8 应用程序?

objective-c - 多线程核心数据 iOS 崩溃

ios - NSManagedObject 集合

xcode - 核心数据错误: "Can' t find model for source store"

ios - RestKit 序列化创建重复条目

ios8 - 在 iOS8 自定义键盘中识别主机应用程序