我正在创建一个程序,要求我构建一个NSMutable数组,然后在单击按钮时将对象添加到该数组。
用户单击了他们想要的所有按钮(因此将所需的所有对象添加到可变数组中)后,我需要根据对象名称对数组进行排序。 对象是NSImageViews,它们被添加到数组中,其名称类似于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>
答案 0 :(得分:3)
您的数组包含UIImageView
和您在caseInsensitiveCompare:
的实例上的排序UIImageView
,但该类没有该名称的任何方法。你应该这样做:
[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
}];