sorting an NSMutableArray of objects in cocoa gives SIGABRT

0

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>
objective-c
cocoa-touch
cocoa
sorting
nsmutablearray
asked on Stack Overflow Sep 30, 2011 by Thermo • edited Sep 30, 2011 by Peter Hosey

1 Answer

3

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
}];
answered on Stack Overflow Sep 30, 2011 by DarkDust

User contributions licensed under CC BY-SA 3.0