我是一名C#软件开发人员,所以我知道如何在Windows上进行编程,但我是OS X,XCode和Swift的全新手。
我编写了一个使用名为“SharesEntity”的Core Data实体的代码,我尝试使用NSExpression来确定属性的最大值,称为“sorrend”(订单)。但是当我执行代码时出现错误:
“无法识别的选择器发送到实例0x127”
此处发生错误:尝试orderResults = context!.executeFetchRequest(orderRequest)
var appDel : AppDelegate?
var context : NSManagedObjectContext?
var iOrder : Int32 = 0
override func viewDidLoad() {
super.viewDidLoad()
appDel = (NSApplication.sharedApplication().delegate as! AppDelegate)
context = appDel!.managedObjectContext
betoltes()
}
func sorrend() {
let orderRequest = NSFetchRequest(entityName: "SharesEntity")
orderRequest.resultType = NSFetchRequestResultType.DictionaryResultType
let keyPathExpression = NSExpression(forKeyPath: "sorrend")
let maxExpression = NSExpression(forFunction: "max:", arguments: [keyPathExpression])
let expressionDescription = NSExpressionDescription()
expressionDescription.name = "maxSorrend"
expressionDescription.expression = maxExpression
expressionDescription.expressionResultType = NSAttributeType.Integer32AttributeType
var expressionDescriptions = [AnyObject]()
expressionDescriptions.append(expressionDescription)
orderRequest.propertiesToFetch = expressionDescriptions
var orderResults:NSArray?
do {
try orderResults = context!.executeFetchRequest(orderRequest)
} catch {
print("Oops! Error occured.")
}
if (orderResults!.count > 0) {
iOrder = orderResults![0].valueForKey("maxSorrend") as! Int32
}
print("Sorrend: ", iOrder)
}
错误堆栈:
Printing description of orderRequest:
<NSFetchRequest: 0x6100000cce10> (entity: SharesEntity; predicate: ((null)); sortDescriptors: ((null)); type: NSDictionaryResultType; includesPendingChanges: NO; propertiesToFetch: ((
"(<NSExpressionDescription: 0x610000163a80>), name maxSorrend, isOptional 1, isTransient 0, entity (null), renamingIdentifier maxSorrend, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}"
)); )
2016-04-23 17:30:33.436 iShares[2448:156683] -[__NSCFNumber count]: unrecognized selector sent to instance 0x127
2016-04-23 17:30:33.437 iShares[2448:156683] -[__NSCFNumber count]: unrecognized selector sent to instance 0x127
2016-04-23 17:30:33.440 iShares[2448:156683] (
0 CoreFoundation 0x00007fff82e284f2 __exceptionPreprocess + 178
1 libobjc.A.dylib 0x00007fff97b22f7e objc_exception_throw + 48
2 CoreFoundation 0x00007fff82e921ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff82d98571 ___forwarding___ + 1009
4 CoreFoundation 0x00007fff82d980f8 _CF_forwarding_prep_0 + 120
5 Foundation 0x00007fff899f8579 +[_NSPredicateUtilities max:] + 56
6 Foundation 0x00007fff8981c5fb -[NSFunctionExpression expressionValueWithObject:context:] + 1094
7 CoreData 0x00007fff90648f8a -[NSMappedObjectStore executeFetchRequest:withContext:] + 2122
8 CoreData 0x00007fff906a3288 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 4120
9 CoreData 0x00007fff906adb63 gutsOfBlockToNSPersistentStoreCoordinatorPerform + 179
10 libdispatch.dylib 0x00000001005cdcc5 _dispatch_client_callout + 8
11 libdispatch.dylib 0x00000001005cf3db _dispatch_barrier_sync_f_invoke + 375
12 CoreData 0x00007fff9069c662 _perform + 194
13 CoreData 0x00007fff905e05c5 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 533
14 CoreData 0x00007fff905ded4a -[NSManagedObjectContext executeFetchRequest:error:] + 586
15 iShares 0x00000001000040a7 _TFC7iShares14ViewController7sorrendfT_T_ + 1047
16 iShares 0x0000000100002ee0 _TFC7iShares14ViewController9hozzaadasfPs9AnyObject_T_ + 64
17 iShares 0x0000000100003c76 _TToFC7iShares14ViewController9hozzaadasfPs9AnyObject_T_ + 54
18 libsystem_trace.dylib 0x00007fff92e1707a _os_activity_initiate + 75
19 AppKit 0x00007fff8c7fce89 -[NSApplication sendAction:to:from:] + 460
20 AppKit 0x00007fff8c80efde -[NSControl sendAction:to:] + 86
21 AppKit 0x00007fff8c80ef08 __26-[NSCell _sendActionFrom:]_block_invoke + 131
22 libsystem_trace.dylib 0x00007fff92e1707a _os_activity_initiate + 75
23 AppKit 0x00007fff8c80ee65 -[NSCell _sendActionFrom:] + 144
24 libsystem_trace.dylib 0x00007fff92e1707a _os_activity_initiate + 75
25 AppKit 0x00007fff8c80d48a -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2693
26 AppKit 0x00007fff8c855fd0 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 744
27 AppKit 0x00007fff8c80bbb4 -[NSControl mouseDown:] + 669
28 AppKit 0x00007fff8cd60469 -[NSWindow _handleMouseDownEvent:isDelayedEvent:] + 6322
29 AppKit 0x00007fff8cd6144d -[NSWindow _reallySendEvent:isDelayedEvent:] + 212
30 AppKit 0x00007fff8c7a063d -[NSWindow sendEvent:] + 517
31 AppKit 0x00007fff8c720b3c -[NSApplication sendEvent:] + 2540
32 AppKit 0x00007fff8c587ef6 -[NSApplication run] + 796
33 AppKit 0x00007fff8c55146c NSApplicationMain + 1176
34 iShares 0x000000010000b264 main + 84
35 libdyld.dylib 0x00007fff921315ad start + 1
36 ??? 0x0000000000000003 0x0 + 3
)
+ [_ NSPredicateUtilities max:]的断点结果:
Foundation`+[_NSPredicateUtilities max:]:
0x7fff899f8541 <+0>: pushq %rbp ; this line was highlighted
0x7fff899f8542 <+1>: movq %rsp, %rbp
0x7fff899f8545 <+4>: pushq %r15
0x7fff899f8547 <+6>: pushq %r14
0x7fff899f8549 <+8>: pushq %r13
0x7fff899f854b <+10>: pushq %r12
0x7fff899f854d <+12>: pushq %rbx
0x7fff899f854e <+13>: subq $0x258, %rsp ; imm = 0x258
0x7fff899f8555 <+20>: movq %rdx, %r14
0x7fff899f8558 <+23>: movq %rdi, %rbx
0x7fff899f855b <+26>: movq -0x163ec1da(%rip), %rax ; (void *)0x00007fff735cc070: __stack_chk_guard
0x7fff899f8562 <+33>: movq (%rax), %rax
0x7fff899f8565 <+36>: movq %rax, -0x30(%rbp)
0x7fff899f8569 <+40>: movq -0x163336d0(%rip), %rsi ; "count"
0x7fff899f8570 <+47>: movq %r14, %rdi
0x7fff899f8573 <+50>: callq *-0x163ebbf9(%rip) ; (void *)0x00007fff97b134c0: objc_msgSend
0x7fff899f8579 <+56>: testq %rax, %rax
0x7fff899f857c <+59>: je 0x7fff899f8947 ; <+1030>
0x7fff899f8582 <+65>: movq -0x1632cf79(%rip), %rsi ; "_getCommonTypeFor:"
0x7fff899f8589 <+72>: movq -0x163ebc10(%rip), %r15 ; (void *)0x00007fff97b134c0: objc_msgSend
...
我每天使用Google,但到目前为止找不到解决方案。谢谢你的帮助。