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

发布于 2024-12-07 13:32:45 字数 2276 浏览 0 评论 0原文

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

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

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

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

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

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

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

2011-09-30 08:16:06.669 CAP helper[9874:b303] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIImageView caseInsensitiveCompare:]:发送到无法识别的选择器实例 0x4e5f650'
*** 第一次抛出时的调用堆栈:
(
0 核心基础 0x00de05a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f34313 objc_异常_抛出 + 44
2 CoreFoundation 0x00de20bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 核心基础 0x00d51966 ___转发___ + 966
4 核心基础 0x00d51522 _CF_forwarding_prep_0 + 50
5 核心基础 0x00d472f6 __CFSimpleMergeSort + 374
6 CoreFoundation 0x00d4706c CFSortIndexes + 268
7 CoreFoundation 0x00dda642 -[NSMutableArray sortRange:选项:usingComparator:] + 274
8 CoreFoundation 0x00d598cf -[NSMutableArray sortWithOptions:usingComparator:] + 95
9 CoreFoundation 0x00d5983c -[NSMutableArray sortUsingSelector:] + 108
10 CAP 助手 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(内部)_sendActionsForEvents:withEvent:] + 527
14 UIKit 0x000c37d8 -[UIControl TouchsEnded:withEvent:] + 458
15 UIKit 0x00056ded - [UIWindow _sendTouchesForEvent:] + 567
16 UIKit 0x00037c37-[UIApplication 发送事件:] + 447
17 UIKit 0x0003cf2e _UIApplicationHandleEvent + 7576
18 图形服务 0x01019992 PurpleEventCallback + 1550
19 核心基础 0x00dc1944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
20 核心基础 0x00d21cf7 __CFRunLoopDoSource1 + 215
21 核心基础 0x00d1ef83 __CFRunLoopRun + 979
22 核心基础 0x00d1e840 CFRunLoopRunSpecific + 208
23 核心基础 0x00d1e761 CFRunLoopRunInMode + 97
24 图形服务 0x010181c4 GSEventRunModal + 217
25 图形服务 0x01018289 GSEventRun + 115
26 UIKit 0x00040c93 UIApplicationMain + 1160
27 CAP 助手 0x00002219 主 + 121
28 CAP 助手 0x00002195 开始 + 53
29 ??? 0x00000001 0x0 + 1
)
终止调用抛出异常
当前语言:自动;目前objective-c>

I am creating a program that requires me to build an NSMutable array and then add objects to that array as buttons are clicked.

After the user has clicked all the buttons that they want (therefore adding all the objects that they need into the mutable array) I need to sort the array based on the objects name.
The objects are NSImageViews that are added to the array with names like view1, view2, view40.
I need a way to sort the objects in the array according to the numbers in the last digit of their name.
I was using

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

but when I run the app I get SIGABRT at that line.

I have looked at many many other threads and can not find a solution.

Any help is greatly appreciated, as I have been working on this for several weeks now,
Anybody know what I am doing wrong?

EDIT , Here is what the debugger said afterwords

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女山的月亮 2024-12-14 13:32:45

您的数组包含 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
}];

Your array contains UIImageViews and your sort calls caseInsensitiveCompare: on those instances of UIImageView, but that class doesn't have any method by that name. You should do something like this:

[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
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文