__NSCFConstantString objectForKey unrecognized selector sent to instance error - ios

I basically get this error
'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x581f0'
on my program. I think it refers to this call I make,
if (data != nil) {
if([data objectForKey:#"username"]){
// NSArray *check= [[NSArray alloc]init];
//check=[data allValues];
[dict setObject:[data allValues] forKey:#"args"];
}else{
[dict setObject:[NSArray arrayWithObject:data] forKey:#"args"];
}
at the setObject:[data allValues]. I don't know why it gives that error but data is an NSDictionary and I'm getting all the values and placing it in an array.

Is the error happening here:
if([data objectForKey:#"username"]){
I assume so, as that is the only place objectForKey seems to be called. You are calling it on a variable called 'data', which i'm guessing simply is not a dictionary. You should NSLog its type to see.

Related

iOS crash : [__NSArrayM allKeys]: unrecognized selector sent to instance 0x178754d0

A user got this crash [__NSArrayM allKeys]: unrecognized selector sent to instance 0x178754d0
This is where the crash occurred.
NSArray *sortedArray = [[array allKeys] sortedArrayUsingFunction:sort context:nil];
I am not sure how this can occur. Any tips or suggestions on how to prevent this will be appreciated.
Edit:
This is my array, I should change the variable name.
id array = [parse objectWithString:answer];
What instance type is array? NSArray has no allKeys message that it can send, thus the unrecognized selector. You are treating array most likely as an NSDictionary.
allKeys function is of NSDictionary not NSArray or NSMutableArray.
You should call this on NSDictionary instances.

Parsing JSON Array in iOS

I'm attempting to read a JSON array from my php generated file at www.tiritium.com/standings.php and it works just fine on Android but I cannot get it to read out of the file. It opens it properly but I can't get get the NSDictionary into a NSArray.
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* latestNames = [json objectForKey:#"name"]; //2
NSLog(#"name: %#", latestNames); //3
I've narrowed down the error to //2 but I can't seem to figure out what is going on. I'm not sure if it is with how my data is being output from the script or with my code in the app itself.
2013-08-14 16:39:51.992 Derby Days[7951:c07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a297b0
2013-08-14 16:39:51.993 Derby Days[7951:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a297b0'
Is the error message I am receiving.
Read the exception
-[__NSCFArray objectForKey:]: unrecognized selector
The root of your JSON is an array, not a dictionary.
Your JSON file represents an array, not a dictionary. If you need to get an array of names out of it, you use key value coding like NSArray *names = [json valueForKey:#"name"];. This will give you an array of NSStrings.

Why am I getting "[__NSArrayI allKeys]: unrecognized selector sent to instance" / why is NSDictionary transforming?

Looking this [__NSArrayI allKeys]: unrecognized selector sent to instance error up, it seemingly occurs when you send an NSArray the allKeys message which is meant for NSDictionary, but in this case I'm very clearly sending it to an NSDictionary.
Here's the code I use when interfacing with the Pocket API:
NSDictionary *articles = [response objectForKey:#"list"];
// Create an array we can use to sort the keys (and thus the articles) in order of when they were added
NSMutableArray *allKeys = [[articles allKeys] mutableCopy];
The last line there causes the error. But articles is very clearly declared as an NSDictionary? Why is it not liking it?
Oddly enough, if I inspect it at runtime it says it's an NSArray! Why did it change?
(lldb) po articles
$5 = 0x082103e0 <__NSArrayI 0x82103e0>(
)
(lldb) po [articles class]
$6 = 0x01b83b8c __NSArrayI
(lldb)
It may be declared in your code as a dictionary, but that doesn't make it a dictionary. It is truly an array and that is why you get the exception. Check your response so you know what you should expect.
Your code compiles because the compiler doesn't know that it's going to be an array and it trusts you that it will be a dictionary. It does this because objectForKey: returns id.

Removing an object from a mutable dictionary throws an exception

I'm getting an exception after attempting to remove an object from a NSMutableDictionary. The relevant code follows. The 'settings' is passed to the method and can be a NSDictionary or a NSMutableDictionary.
NSMutableDictionary *mutableSettings = nil;
if ([settings isKindOfClass:[NSMutableDictionary class]])
mutableSettings = (NSMutableDictionary *)settings;
else
mutableSettings = [[[NSMutableDictionary alloc] initWithDictionary:settings] autorelease];
[mutableSettings removeObjectForKey:#"akey"];
This crashes with
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary removeObjectForKey:]: mutating method sent to immutable object'
Whats wrong with this? Thanks.
The problem here is that both NSDictionary and NSMutableDictionary return __NSCFDictionary as their class, due to the fact that NSDictionary is a class cluster.
I think you will just have to make a mutable copy of the settings dictionary whether it is mutable or not.
NSMutableDictionary *mutableSettings = [settings mutableCopy];

NSArray SIGABRT

NSString* year = [self.years objectAtIndex:[indexPath section]];
//get algorithms for that year
NSArray* algorithmSection = [self.algorithmNames objectForKey:year];
NSLog(#"%#", indexPath);
//get specific algorithm based on that row
cell.textLabel.text = [algorithmSection objectAtIndex:indexPath.row];
return cell;
For whatever reason, when I compile this, I get a SIGABRT error. It happens on the
cell.textLabel.text
line. Error:
2011-08-29 19:26:21.273 xxxxxxxxxxx[1431:b303] 2 indexes [0, 0]
2011-08-29 19:26:21.274 xxxxxxxxxxxxx[1431:b303] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4ba2370
2011-08-29 19:26:21.277 xxxxxxxxx[1431:b303] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4ba2370'
terminate called throwing an exception
Your algorithmSection variable, which your code expects to be an NSArray, is actually an NSDictionary, which does not respond to the selector -objectAtIndex:.
As lemnar and mjisawai said, you are actually dealing with an NSDictionary.
The way to fix this depends on the context of your app.
If you happen to receive either, NSArrays or NSDictionary objects then you may determine this by texting the object's class.
i.e.
if ([algorithmSection isKindOfClass:[NSArray class]]) {
...
} else if ([algorithmSection isKindOfClass:[NSDictionary class]]) {
...
}
Could the object at key "year" in your algorithmSections dictionary be a dictionary instead of an array? That's what it looks like is happening here.

Resources