NSArray SIGABRT - ios

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.

Related

Crash when assigning null to NSString from JSON data

I am experiencing a crash that I have narrowed down to this line of code but I dont understand how I can avoid it.
NSString *variantImageUrl = variantEdge[#"node"][#"image"][#"src"];
If the value of src is null the app crashes with the following error message
2017-11-21 17:23:27.023988+0800 NWMPos[3218:2124447] -[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1b2a01650
2017-11-21 17:23:27.026199+0800 NWMPos[3218:2124447] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1b2a01650'
As long as the value of src is a proper string it works fine
But I cannot check the value for null before I assign it so how can avoid the crash?
Add check for null:
if(![variantEdge[#"node"][#"image"][#"src"] isEqual:[NSNull null]])
{
NSString *variantImageUrl = variantEdge[#"node"][#"image"][#"src"];
}
Clumsy as hell, but
NSDictionary *dict = variantEdge;
for (NSString *key in #[#"node", #"image"]) {
dict = dict[key];
if (![dict isKindOfClass:NSDictionary.class]) {
dict = nil;
break;
}
}
NSString *url = dict ? dict["src"] : nil;

NSArray valueForKey ObjectAtIndex

I have an NSArray that i want a single value of.
_membersArray = [_detailItem valueForKey:#"members"];
When i use this code:
NSLog(#"%lu", (unsigned long)[[_membersArray valueForKey:#"name"] count ]);
i get: 1
When i use this one:
NSLog(#"%#", [_membersArray valueForKey:#"name"] );
i get
{(
"Name Lastname"
)}
But i need only "Name lastname"
And when i use:
NSLog(#"%#", [[_membersArray valueForKey:#"name"] objectAtIndex:0]);
i get:
[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x10bf5caf0
2014-03-13 12:35:39.261 Givem[2454:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x10bf5caf0'
i guess this is a easy one but i have a codeblock. Thanks for helping me out
Then try this..
[[[_membersArray valueForKey:#"name"] allObjects]objectAtIndex:0];
Look at exception ... It says ...
[__NSSetI objectAtIndex:] ...
... it means, that you're trying to send objectAtIndex: message to NSSet, not NSArray. And NSSet doesn't have objectAtIndex: method.

__NSCFConstantString objectForKey unrecognized selector sent to instance error

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.

"[__NSArrayM objectForKey:]: unrecognized selector" when trying to retrieve a string from Dictionary

I am trying to set text to my labels. Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
newsDataCell *cell = (newsDataCell*)[tableView dequeueReusableCellWithIdentifier:#"newsData"];
if (cell == nil)
{
cell = [[newsDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"newsData"];
}
NSDictionary* currentrow =[self.Items objectAtIndex:indexPath.item];
NSLog(#"currentrow:%#",currentrow);
//UIImageView *image = (UIImageView*)[cell viewWithTag:0];
UILabel *txtData = (UILabel*)[cell viewWithTag:1];
NSString *testo = [currentrow objectForKey:#"Date"];
[txtData setText:testo];
UILabel *txtTitolo = (UILabel*)[cell viewWithTag:2];
txtTitolo.text =[currentrow valueForKey:#"Title"];
UILabel *txtDescrizione = (UILabel*)[cell viewWithTag:3];
txtDescrizione.text = [currentrow valueForKey:#"Descr"];
return cell;
}
I checked and currentrow actually contains the object.
This is the error I get from Xcode:
2013-09-11 16:04:36.468 ApplicationName[2409:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]:
unrecognized selector sent to instance 0x7172f80'
*** First throw call stack:
(0x1333012 0x1158e7e 0x13be4bd 0x1322bbc 0x132294e 0xc5f4 0x1578fb 0x1579cf 0x1401bb 0x150b4b 0xed2dd 0x116c6b0 0x265bfc0 0x265033c 0x2650150 0x25ce0bc 0x25cf227
0x25cf8e2 0x12fbafe 0x12fba3d 0x12d97c2 0x12d8f44 0x12d8e1b 0x22757e3 0x2275668 0x9cffc 0x2c2d 0x2b55)
libc++abi.dylib: terminate called throwing an exception
I get the same error using valueForKey:
txtTitolo.text =[currentrow valueForKey:#"Title"];
Here it is:
2013-09-11 16:09:34.478 ApplicationName[2409:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM valueForKey:]:
unrecognized selector sent to instance 0x7172f80'
*** First throw call stack:
(0x1333012 0x1158e7e 0x13be4bd 0x1322bbc 0x132294e 0xc5f4 0x1578fb 0x1579cf 0x1401bb 0x150b4b 0xed2dd 0x116c6b0 0x265bfc0 0x265033c 0x2650150 0x25ce0bc 0x25cf227
0x25cf8e2 0x12fbafe 0x12fba3d 0x12d97c2 0x12d8f44 0x12d8e1b 0x22757e3 0x2275668 0x9cffc 0x2c2d 0x2b55)
libc++abi.dylib: terminate called throwing an exception
What is wrong?
Looks like currentrow is not a NSDictionary (which you are assuming). It is an NSArray.
NSDictionary* currentrow =[self.Items objectAtIndex:indexPath.item];
// Not a dictionary, it is an array.
That is why you are getting this crash. If this is a parsed JSON response, check the structure of JSON.
Hope that helps!
I did it! I just used "stringWithFormat" and it worked like this:
NSString *testo = [NSString stringWithFormat:#"%#",[currentrow objectForKey:#"Date"]];
Thank you anyway for your answers!
A. This error is not produced by Xcode but by the runtime environment.
B. It means that currentrow is not a dictionary but an array.
C. How did you check it? Where do you create the items and put it in the list?
For me this is worked.
customerBillerFieldsArray = [self.detailItem valueForKey:#"customerBillerFileds"];
Earlier I used customerBillerFieldsArray = [self.detailItem objectForKey:#"customerBillerFileds"];

Why do i get an error comparing NSString's? (-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance)

I have a NSMutableArray (_theListOfAllQuestions) that I am populating with numbers from a file. Then I compared the objects in that array with qNr (NSString) and I got error. I even casted the array to another NSString, _checkQuestions, just to be sure I am comparing NSStrings. I tested using item to compare also.
-(void)read_A_Question:(NSString *)qNr {
NSLog(#"read_A_Question: %#", qNr);
int counter = 0;
for (NSString *item in _theListOfAllQuestions) {
NSLog(#"item: %#", item);
_checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString
NSLog(#"_checkQuestions: %#", _checkQuestions);
if ([_checkQuestions isEqualToString:qNr]) {
NSLog(#">>HIT<<");
exit(0); //Just for the testing
}
counter++;
}
When running this code i get the following NSLog:
read_A_Question: 421
item: 1193
_checkQuestions: 1193
...and error:
-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9246d80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber
isEqualToString:]: unrecognized selector sent to instance 0x9246d80'
I do believe that I still comparing NSString with a number of some sort but to me it looks like I am comparing NSString vs. NSString?
I could really need some help here to 1) understand the problem, 2)solve the problem?
Replace this line
if ([_checkQuestions isEqualToString:qNr])
with
if ([[NSString stringWithFormat:#"%#",_checkQuestions] isEqualToString:[NSString stringWithFormat:#"%#",qNr]])
Hope it helps you..
Your _theListOfAllQuestions array has NSNumber objects and not NSString objects. So you cant use isEqualToString directly.
Try this,
for (NSString *item in _theListOfAllQuestions) {
NSLog(#"item: %#", item);
_checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString
NSLog(#"_checkQuestions: %#", _checkQuestions);
if ([[_checkQuestions stringValue] isEqualToString:qNr]) {
NSLog(#">>HIT<<");
exit(0); //Just for the testing
}
counter++;
}

Resources