performSelector on NSMutableArray error
-
01-07-2021 - |
Question
Hello I have a tricky trouble with performSelector on NSMutableArray. It is possible I misunderstood what performSelector does. Basically i have NSMutableArray with objects (all of them are objects based on my class which inherits from UIButton, but that is not important i guess) and i need send message to all objects in array.
In header file of my class i have declared function, which is called by selector. It looks like this:
-(void)scaleChangedWithANumber:(NSNumber *)scale;
In this example my array contains 3 objects. If i call them item by item, it works perfect:
NSNumber *tmpNumber=[NSNumber numberWithFloat:scale];
ButtonOfElement *tmpButton1=[elementsButtonContainer objectAtIndex:0];
[tmpButton1 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
ButtonOfElement *tmpButton2=[elementsButtonContainer objectAtIndex:1];
[tmpButton2 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
ButtonOfElement *tmpButton3=[elementsButtonContainer objectAtIndex:2];
[tmpButton3 performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
But if I try to call performSelector on array (i need to send message to all of objects in array):
NSNumber *tmpNumber=[NSNumber numberWithFloat:scale];
[elementsButtonContainer performSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
tmpNumber=nil;
I get error:
[__NSArrayM scaleChangedWithANumber:]: unrecognized selector sent to instance 0x1737f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM scaleChangedWithANumber:]: unrecognized selector sent to instance 0x1737f0'
*** First throw call stack:
(0x34e456c3 0x370be97f 0x34e49227 0x34e47951 0x34d9f468 0x31d1 0x369a70eb 0x369a7447 0x369324d9 0x368f9c0d 0x36ae5649 0x3681df4f 0x3681c9b3 0x3682791f 0x368274eb 0x36814f29 0x36814843 0x337425d3 0x33742203 0x34e1a593 0x34e1a537 0x34e193b9 0x34d8c39d 0x34d8c229 0x3374131b 0x368688f9 0x2363 0x2308)
libc++abi.dylib: terminate called throwing an exception
I can use loop and call perform selector item by item of course, but i thought it will be more elegant and clearer to use performSelector on array.
La solution
Try this:
[elementsButtonContainer makeObjectsPerformSelector:@selector(scaleChangedWithANumber:) withObject:tmpNumber];
Autres conseils
elementsButtonContainer
doesn't have a method called scaleChangedWithANumber
. You need to declare it with some different logic, or call the existing method on all button subclasses in the array.