NSDictionary element to UITextfield - ios

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.

Related

NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x165d5150'

Hi I am getting this data form server
NSDictionary*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed); //Outputs: feed = ( { code = yQ7j0t; "user_id" = 889445341091863; } ); }
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;
And beacuse of that I am getting below error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: selector sent to instance 0x165d5150'``
Any help will be appreciated.
The issue is, your feed key holds an array. You are not properly handling that in your code, that is why the crash occurs. When you call valueForKey: it retrieves an array of values held by that specific key.
For fixing that you can use:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSArray *referralCodes = [feed valueForKey:#"code"];
NSString *referralCode = referralCodes.count ? referralCodes[0] : #"";
NSLog(#"%#",referralCode);
But I personally prefer using objectForKey: instead of valueForKey:. So you can re-write the code like:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSString *referralCode = feed.count ? [feed[0] objectForKey:#"code"] : #"";
NSLog(#"%#",referralCode);
Some where you use a variable;
yourVaribleName.length
or
[yourVaribleName length]
which should be
yourVaribleName.count
note: the crash says exactly that "yourVaribleName" is NSArray type where you wants length of the NSArray. But NSArray has not feature "length". NSArray has "Count" feature
//try with this code bellow
NSArray *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=[referralCode componentsJoinedByString:#" "];//#"," or #"" what event you need
Your feed data is in array. So you have retrieve code value from array.Hope it will help you.
NSMutableArray*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed);
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [[feed objectAtIndex:indexPath]valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;

unrecognized selector on NSArray : 'NSInvalidArgumentException'

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.

__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.

crash when using NSNumberFormatter to convert NSString to NSNumber

I have a simple value in an NSSTRING that I want to convert to an NSNumber. I do this all the time in my code and for some reason, this time it is not working. Do you see anything wrong with this?
NSNumberFormatter * num_formatter = [[NSNumberFormatter alloc] init];
[num_formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *score;
NSString *mystr = [[player ScoresArray] objectAtIndex:currentKeeper - 1];
NSLog(#"here is my string: -%#-", mystr);
score = [num_formatter numberFromString:mystr]; // crash occurs on this line. see error below...
NSLog(#"now it is: %d", [score intValue]); // it never gets to this line...
Here is the output from the above code:
here is my string: -3-
Here is the error I get:
2013-02-26 17:21:48.912 Golf Whiz[50407:c07] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xcb5fa90
2013-02-26 17:21:48.912 Golf Whiz[50407:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xcb5fa90'
*** First throw call stack:
Make sure the objects you're adding to ScoresArray are in fact strings, not NSNumbers.
Thanks, all, as suspected, it turns out that I did have nsnumber objects in the scoresArray afterall.

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