gpt4 book ai didi

objective-c - 对 cocoa 中的对象 NSMutableArray 进行排序会给出 SIGABRT

转载 作者:行者123 更新时间:2023-12-03 17:35:59 25 4
gpt4 key购买 nike

我正在创建一个程序,需要我构建一个 NSMutable 数组,然后在单击按钮时将对象添加到该数组。

用户单击他们想要的所有按钮后(因此将他们需要的所有对象添加到可变数组中),我需要根据对象名称对数组进行排序。这些对象是 NSImageView,它们被添加到数组中,名称如 view1、view2、view40。我需要一种方法根据对象名称最后一位数字对数组中的对象进行排序。我正在使用

[nameOfArray sortUsingSelector:@selector(caseInsensitiveCompare:)];

但是当我运行应用程序时,我在该行收到 SIGABRT。

我查看了许多其他线程,但找不到解决方案。

非常感谢任何帮助,因为我已经为此工作了几个星期,有人知道我做错了什么吗?

编辑,这是调试器后来说的内容

2011-09-30 08:16:06.669 CAP helper[9874:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView caseInsensitiveCompare:]: unrecognized selector sent to instance 0x4e5f650'
*** Call stack at first throw:
(
0 CoreFoundation 0x00de05a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f34313 objc_exception_throw + 44
2 CoreFoundation 0x00de20bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d51966 ___forwarding___ + 966
4 CoreFoundation 0x00d51522 _CF_forwarding_prep_0 + 50
5 CoreFoundation 0x00d472f6 __CFSimpleMergeSort + 374
6 CoreFoundation 0x00d4706c CFSortIndexes + 268
7 CoreFoundation 0x00dda642 -[NSMutableArray sortRange:options:usingComparator:] + 274
8 CoreFoundation 0x00d598cf -[NSMutableArray sortWithOptions:usingComparator:] + 95
9 CoreFoundation 0x00d5983c -[NSMutableArray sortUsingSelector:] + 108
10 CAP helper 0x0000cf81 -[RibbonStacker stackit:] + 305
11 UIKit 0x000324fd -[UIApplication sendAction:to:from:forEvent:] + 119
12 UIKit 0x000c2799 -[UIControl sendAction:to:forEvent:] + 67
13 UIKit 0x000c4c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
14 UIKit 0x000c37d8 -[UIControl touchesEnded:withEvent:] + 458
15 UIKit 0x00056ded -[UIWindow _sendTouchesForEvent:] + 567
16 UIKit 0x00037c37 -[UIApplication sendEvent:] + 447
17 UIKit 0x0003cf2e _UIApplicationHandleEvent + 7576
18 GraphicsServices 0x01019992 PurpleEventCallback + 1550
19 CoreFoundation 0x00dc1944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
20 CoreFoundation 0x00d21cf7 __CFRunLoopDoSource1 + 215
21 CoreFoundation 0x00d1ef83 __CFRunLoopRun + 979
22 CoreFoundation 0x00d1e840 CFRunLoopRunSpecific + 208
23 CoreFoundation 0x00d1e761 CFRunLoopRunInMode + 97
24 GraphicsServices 0x010181c4 GSEventRunModal + 217
25 GraphicsServices 0x01018289 GSEventRun + 115
26 UIKit 0x00040c93 UIApplicationMain + 1160
27 CAP helper 0x00002219 main + 121
28 CAP helper 0x00002195 start + 53
29 ??? 0x00000001 0x0 + 1
)
terminate called throwing an exception
Current language: auto; currently objective-c>

最佳答案

您的数组包含 UIImageView,并且您的排序在 UIImageView 的这些实例上调用 caseInsensitiveCompare:,但该类没有任何方法以这个名字。你应该做这样的事情:

[nameOfArray sortUsingComparator:^(id obj1, id obj2) {
UIImageView *view1 = obj1;
UIImageView *view2 = obj2;

// Somehow compare the views and return NSOrderedAscending,
// NSOrderedDescending or NSOrderedSame, for example by calling
// appropriate "compare:" methods.
return myComparisonResult
}];

关于objective-c - 对 cocoa 中的对象 NSMutableArray 进行排序会给出 SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7611859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com