unrecognized selector on NSArray : 'NSInvalidArgumentException' - ios

I am sitting since a hour on some strange exception.
I try to call some method with:
for (int i = 0; i < [[DBElements objectAtIndex:index] count]; i++) {
NSLog(#"selected Element: %#", [[DBElements objectAtIndex:index] objectAtIndex:i]);
[self addElementsToView:dash withString:[[DBElements objectAtIndex:index] objectAtIndex:i] index:index andSubindex:i];
}
the method is of types: - (void) addElementsToView: (UIView *) dash withString: (NSString *) type index:(NSInteger)index andSubindex : (int) i {}
NSLog shows me:
selected Element: NUMBER
so the index stuff is ok.
Why I get on the next step the following exception:
[__NSArrayI intValue]: unrecognized selector sent to instance
0x14d44f70 * Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSArrayI intValue]:
unrecognized selector sent to instance 0x14d44f70'
UPDATE: The DBElements is:
(
(
NUMBER,
LABEL
),
LABEL
)
index is 0
i is 0. so it shows to NUMBERS of type NSString.

Try to cast
withString:[[DBElements objectAtIndex:index] objectAtIndex:i]
like this:
withString:(NSString *)[[DBElements objectAtIndex:index] objectAtIndex:i]
Hope it helps!

Somewhere in your code you are calling intValue on a NSArray. Maybe in the addElementsToView method.
Looking at the (short) code sample I assume it has something to do with DBElements. You probably want to call intValue on a NSString to convert it to number value.
If you have an Arrays nested in Arrays then make sure you're calling the right selectors on the right objects.

Related

Throwing Exception when sum up the response array values

Hi I'm getting following exception when I try to sum up the values of a response array from both the method mentioned below.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI intValue]: unrecognized selector sent to instance 0x1c463ade0'
Tried code below
-(void)getSelectedServiceTypeArray:(NSArray *)selectedServicesTypeArray
{
NSInteger sum = 0;
selectedMultiServiceType = [[NSArray alloc]initWithObjects:selectedServicesTypeArray, nil];
NSArray *priceArray = [selectedMultiServiceType valueForKey:#"price_per_unit"];
NSLog (#"PriceArray%#",priceArray);
// sum = [priceArray valueForKeyPath:#"#sum.self"];
for (NSNumber *num in priceArray)
{
sum += [num intValue];
NSLog(#"SUMR1%ld",(long)sum);
}
NSLog(#"SSTAA%#",selectedMultiServiceType);
}
Mean while I tried both the method for an array contains manually entered values. Anyway Both methods works for this..
NSArray *testArray = #[#"24",#"75",#"80",#"44"];
NSNumber *sum = [testArray valueForKeyPath:#"#sum.self"];
NSLog(#"SUM SUM1%#",sum);
double sum2 = 0;
for (NSNumber *serviceFeeTotal in testArray)
{
sum2 += [serviceFeeTotal doubleValue];
}
NSLog(#"SUM SUM2%f",sum2);
I don't why this not working and app is crashing on either of both methods when I using an array that retrieved from a previous ViewController by delegate method.
Response of the array I tried to sum up
PriceArray(
(
30,
45
)
)
Please explain me with a proper code...
Thanks for the All the support I solved the issue by replacing following code
selectedMultiServiceType = [[NSArray alloc]initWithObjects:selectedServicesTypeArray, nil];
**NSArray *priceArray = [selectedMultiServiceType[0] valueForKey:#"price_per_unit"];**
NSLog (#"PriceArray%#",priceArray);

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++;
}

Error setting value of NSMutableDictionary

I initialize this NSMutableDictionary:
define TAREFA #"TAREFA"
define DATA_ITEM #"DATA"
define HORA_ITEM #"HORAS"
define ID #"ID"
define STATE #"NA"
define APROVED #"APROVED"
define REJECTED #"REJECTED"
itensArray = [[NSMutableArray alloc] init];
for (int i=0; i< [listagens size]; i++)
{
NSMutableDictionary *tmpDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[[listagens item:i] WI_ID], ID,
[[listagens item:i] WI_TEXT], TAREFA,
[[listagens item:i] WI_CD], DATA_ITEM,
[[listagens item:i] WI_CT], HORA_ITEM,
STATE, STATE,
nil];
[itensArray addObject:tmpDictionary];
}
Later on, i do the following:
for (int i=0; i<[itensArray count]; i++)
{
if ([[[itensArray objectAtIndex:i] objectForKey:ID] isEqualToString:cell.idLabel.text]) {
[[itensArray objectAtIndex:i] setString:APROVED forKey:STATE];
break;
}
}
I initialize the array, then later on, i change a string of the NSMutableDictionary. When i set the value i get the following runtime error:
'NSInvalidArgumentException', reason: '-[__NSCFDictionary
setString:forKey:]: unrecognized selector sent to instance 0x7b7d170'
It appears i´m not changing the value correctly. Is it a syntax error? How is it done?
maybe you misspelled the name of the method.
// WRONG
[[itensArray objectAtIndex:i] setString:APROVED forKey:STATE];
// GOOD
[[itensArray objectAtIndex:i] setValue:APROVED forKey:STATE];
'NSInvalidArgumentException', reason: '-[__NSCFDictionary setString:forKey:]: unrecognized selector sent to instance 0x7b7d170'
NSMutableDictionary has no setString:forKey: method.
Try setObject:forKey:
[[itensArray objectAtIndex:i] setObject:APROVED forKey:STATE];

NSDictionary element to UITextfield

I have a NSDictionary with some parameters I want to display in a UITextField. but
firstname.text = [userdata objectForKey:#"firstname"];
throws an exeption. If I use NSLog on [userdata objectForKey:#"firstname"]; it shows the right value.
This is the thrown exception:
2012-07-05 15:55:56.533 Project[13642:f803] -[__NSArrayM
_isNaturallyRTL]: unrecognized selector sent to instance 0x68c4e20 2012-07-05 15:55:56.534 Project[13642:f803] *** Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM
_isNaturallyRTL]: unrecognized selector sent to instance 0x68c4e20'
If I use
firstname.text = [NSString stringWithFormat:#"%#",[userdata objectForKey:#"firstname"]];
it works, but it puts the values in brackets.
What seems to be the problem here? And why doesn't the first attempt work?
_isNaturallyRTL
is an undocumented NSString method. It seems that
[userdata objectForKey:#"firstname"]
is an NSMutableArray (guessed from class name), which you're trying to use as a string. When you use a format string with the %# format specifier, it calls the -description method of the object to be formatted/printed (so does NSLog), and the description of an NSArray is an NSString that looks like
[ "description of first element", "description of second element" ]
etc., that's why it puts your text in brackets but doesn't crash.
All in all, use
[[userdata objectForKey:#"firstname"] objectAtIndex:0]
instead.
firstname.text = [[userdata objectForKey:#"firstname"]stringValue];
Does this do any difference?
It would appear that [userdata objectForKey:#"firstname"] is returning an array that contains just the string you are after. Take a look at where you set the value for "firstname" and change it to be just the string that you are after.

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