I have this dictionary defined:
#property (strong,nonatomic,readwrite) NSMutableDictionary* images;
Along with this method:
-(void)initImages
{
UIImage *sprite = [UIImage imageNamed: #"sprite"];
[self.images
setObject:CFBridgingRelease(CGImageCreateWithImageInRect(sprite.CGImage, CGRectMake(0, 0, 64, 64)))
forKey:[NSNumber numberWithInt:black]];
}
And when I call:
[self.images objectForKey:[NSNumber numberWithInt:1]];
I get
2014-03-02 14:51:36.831 kljlkj[7853:70b] -[__NSCFType size]: unrecognized selector sent to instance 0x8b73ba0
Which happens on the line:
[self.images objectForKey:[NSNumber numberWithInt:1]];
If I skip the initImages function call, nothing happens and all is fine, however if I do call initImages I get the exception...
Why is this happening and how do I fix it?
EDIT: Sorry everybody! I was wrong, the exception was in fact thrown on another line than I thought and makes a lot of sense. Sorry again for the waste of time
This exception is means that the method you're calling doesn't exist on the object on which you called it. Somethjng is wrong with self.images when you make that call.
Related
I am getting this error when trying to add the NSString object named object to my array :
erminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7fc48ae52ba0'
Not sure why this is so. I was originally using NSArray to changed it to NSMutableArray but I am still having problems with it. Code is below:
-(NSMutableArray *)getReplyArrayForMessage: (NSString *)message {
//get the array
NSMutableArray *replies = [self.messageDictionary objectForKey:message];
NSLog(#"%#",replies);
//if no replies we init the base set
if([replies count]==0) {
//get the base array
//this also works if a key just isn't in the dictonary
return replies=[self getBaseArray];
}
else {
//add the other message
NSString *object = #"Send a different message";
[replies addObject:object];
return replies;
}
}
If anyone could give me a pointer to why this is happening I would appreciate it. Noob here.
The array is immutable (NSArray), not mutable (NSMutableArray). You can create a mutable array from an immutable array using mutableCopy:
NSMutableArray *replies = [[self.messageDictionary objectForKey:message] mutableCopy];
I know this from the exception text:
-[__NSArrayI addObject:]: unrecognized selector sent to instance ...
^^^^^^^^^^
EDIT I've missed off some important information from my original answer.
Having used mutableCopy you now have a copy of the array and the original array (from self.messageDictionary objectForKey:message) will remain unchanged after you add your element. This is almost certainly not what you intended.
As I've mentioned in the comments below, you probably want to create a custom object to hold these details (or an array of custom objects) that should be created from the message dictionary as soon as you receive it. The effort required to create a custom object will pay for itself tenfold in the long term.
You are trying to add an NSString to an NSArray (which looks like an NSMutableArray) since you initialized it as one. This means that the object that you store in your messageDictionary is actually of type NSArray, and not NSMutabelArray. So, assigning it to an object of type NSMutableArray won't actually change its type and make it mutable.
It's an easy fix:
NSMutableArray *replies = [[NSMutableArray alloc] initWithArray:(NSArray *) [self.messageDictionary objectForKey:message]];
Or you can go with mutableCopy (as suggested by #trojanfoe) which is a shortcut but will lead to the same result.
I can tell because the error message says -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7fc48ae52ba0'. Notice the I at the end of __NSArrayI, this means that this array is actually immutable, so you can't add objects to it.
In my app, I have created a 'NSMutalbeArray', in which I add some 'UILabel'.
I correctly initialize my array :
_pickerMonthLabel = [[NSMutableArray alloc] init];
I correctly add UILabels in my array, but when I want to make some changes to a label, the behavior is strange. For example, I want to change the text color of a label in my array. I do :
[(UILabel *)[_pickerMonthLabel objectAtIndex:i] setTextColor:[UIColor redColor]];
This doesn't look like to work because my app crashes :
-[__NSCFConstantString setTextColor:]: unrecognized selector sent to instance 0x17e8d4
*** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFConstantString setTextColor:]: unrecognized selector sent to instance 0x17e8d4'
But i have no warning when I wrote this line. When I write another kind of line :
(UILabel *)[_pickerMonthLabel objectAtIndex:i].textcolor = [UIColor redColor];
It gives me an error :
Property 'textcolor' not found on object of type 'id'
I don't understand the difference between these two lines, and how to fix my problem...
Thank you in advance for your help.
I guess this is more of a remark, but you could try to use the respondsToSelector: message to check if the instance can handle the message you are trying to send. (This will make sure you don't get an exception) In your case this would be:
if ([[_pickerMonthLabel objectAtIndex:i] respondsToSelector:#selector(setTextColor:)]){
[(UILabel *)[_pickerMonthLabel objectAtIndex:i] setTextColor:[UIColor redColor]];
}
// add this to see what your class is
//else{
// NSLog(#"The class is: %#", [[_pickerMonthLabel objectAtIndex:i] class]);
//}
Hope it helps your debugging.
i have created one custom cell and put button on it
and button event is like this
[cell.BtnRequest addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
and didtapbutton code is this
- (void)didTapButton:(id)sender {
WorkerDetailsVC *wvc=[self.storyboard instantiateViewControllerWithIdentifier:#"workerdetailsvc"];
NSIndexPath *indepath = [self.tblorderdata indexPathForCell:sender];
wvc.arrworkarrequest=[arrData objectAtIndex:indepath.row];
NSLog(#" arr send :%#", wvc.arrworkarrequest);
wvc.struserid = struserid;
[self.navigationController pushViewController:wvc animated:YES];
}
and on the next page i got the array arrworkarrequest and after the load all next page it will show error like this
2014-10-01 15:08:42.607 demo[2151:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860
2014-10-01 15:08:42.613 dmeo[2151:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860'
Your problem is that sender is of type UIButton not UITableViewCell so it's getting stuck in the [self.tblorderdata indexPathForCell:sender] because that method call expects a type of UITableViewCell.
You need to find the Cell that the button is in. You may be able to do this using a while loop (although it's a bit horrible). Something like:
id obj = sender;
while (![obj isKindOfClass:[UITableViewCell class]]) {
obj = [sender superview];
}
[self.tblorderdata indexPathForCell:obj];
This feels like it's abusing the view hierarchy though.
This happens if you compare the length of a string directly
for example
NSString *myString = #"2";
using this statement will generate the error
if(myString.length==2)
I have written simple code to practice selector in Objective C, which is working fine
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"A",#"B", nil];
NSLog(#"Before adding %#",array);
SEL message = #selector(addObject:); //I will change this line
if([array respondsToSelector:message])
{
[array performSelector:message withObject:#"C"];
}
NSLog(#"After adding %#",array);
But as soon as I change selector line to,
SEL message = #selector(addobject:);// Just changed name of selector
XCode starts giving warning :
Undeclared selector 'addobject:'
Now, question is how XCode knows at compile time about name of method is correct or not. Is there always list of selector generates internally for whatever object I am creating? In this case for NSMutableArray
All Xcode knows is that there is no class, either in the system code or in your program, that declares a selector called addobject. You can prove this by creating a custom class that declares an addobject method, and the warning should go away, but of course the program will crash with a unrecognized selector sent to object error message.
I have a problem. I am calling a method in another class. I call it before and it works perfect but I call back in another method near the end of the class and I have this error:
-(void)methodOne:(NSString*)myString
{
mySecondClasss *second = [[mySecondClasss init] autorelease];
[second doSomething:myString];
/*
more code
*/
}
-(void)methodTwo:(NSString*)myString
{
mySecondClasss *second = [[mySecondClasss init] autorelease];
[second doSomething:myString];
/*
more code
*/
}
In the second I'm getting this error:
Exception was thrown: -[mySecondClasss doSomething:]: unrecognized selector sent to instance. I don't understand why works once but not the second time. Any of you can give me some pointers of how can I fix this?
I'll really appreciate your help.
Since IOS 5 you have ARC (Automatic Reference Counting) which is automatically releasing the object correctly. Anyway you are not allocating the memory for the mySecondClasss class. Not sure why it even worked in the first method.
Instead of using
mySecondClasss *second = [[mySecondClasss init] autorelease];
Try using
mySecondClasss *second = [[mySecondClasss alloc] init];
Are you sure that you are sending a string to the function? The error implies that the function is not getting a string.
For example:
[self methodOne:#"Properly formatted string"];