NSDictionary objectForKey returns wrong value - ios

I'm trying to use a dictionary which I got as a JSON response from my server. When I print the description of the dictionary, everything is in order
Printing description of dict:
{
category = 1;
code = 1;
name = "xxxx";
pictureUrl = "xxxx";
sessionId = xxx;
status = 0;
}
I need the "code" value, and when I use objectForKey:#"code" to get it, I get a wrong value:
int code = [NSDictionary objectForKey:#"code"];
After this i print out the value of code and its something like 3483765348, which is very, very wrong.
Why is this happening?

The object returned is an NSNumber and not an int (which isn't an object).
If you want the int value try this
int code = [[myDictionary objectForKey:#"code"] intValue];

Try the following code which will be use to solve your issue.. As per your code it returns only value you have to convert it to int value.Use this piece of code
int code = [[NSDictionary objectForKey:#"code"]intValue];
Hope this Helps !!!

-objectForKey: doesn't return an int, but an object instead (in your case of type NSNumber).
NSNumber *code = [myDict objectForKey:#"code"];
NSInteger codeInteger = [code integerValue];

int code = [[yourResultingDictionary objectForKey:#"code"] intValue];
I think it will be helpful to you.

The object key returns only for array values, so you will use value for key path.
user_idstr = [tweet valueForKeyPath:#"properties.user_id"];

Related

NSDictionary - need to obtain value for key subelement

I have created an NSDictionary named "myData"
which contains the following JSON response:
{
listInfo = (
{
date = 1392157366000;
dateAsString = "02/11/2014 22:22:46";
id = 6;
address = 542160e0000c;
myLevel = 13;
},
{
date = 1392155568000;
dateAsString = "02/11/2014 21:52:48";
id = 5;
address = 542160e0000c;
myLevel = 13;
}
);
}
I need to retrieve each of the [dateAsString] key/value pairs.
I've tried: NSString *dateAsString=[[myData valueForKeyPath:#"dateAsString"][0] objectForKey:#"myData"]; without any luck.
Any suggestions are greatly appreciated.
I think this will work:
NSArray* dateStringArray = [listInfo valueForKeyPath:#"#unionOfObjects.dateAsString"]
I believe it will give you an array of strings. If you need to stuff that back in a dictionary, that should be fairly easy.
It's not clear what your "myData" looks like... so I used the listInfo array of dicts shown.

Number from NSDictionary

This is the code I'm using to pull a number from a dictionary,
int num = [[sharedManager.data objectForKey:#"sum(units.num)"]intValue];
This is the key/value of the number which is printed in the debugger.
key = (__NSCFString *) #"sum(units.num)" 0x176d4d40
value = (__NSCFNumber *) (long)38244 0x176ca850
And this is the result I'm getting,
num = (int) 393051808
What i need is for num to be 38244.
Does anyone know why I'm getting this result?
Thanks
Paul.
What you are getting is , the integer representation (393051808) of your key's memory address (0x176d4d40).
Do this,
NSNumber *number = [sharedManager.data objectForKey:#"sum(units.num)"];
int num = [number intValue];
// or
long num = [number longValue]; //because your number is a long value

How to get value from dictionary

everyone!
Please help me to get the total value from given dictionary
{
"#averageBaseRate" = "296.7";
"#averageRate" = "296.7";
"#commissionableUsdTotal" = "1186.8";
"#currencyCode" = USD;
"#maxNightlyRate" = "296.7";
"#nightlyRateTotal" = "1186.8";
"#total" = "1186.8";
NightlyRatesPerRoom = {
"#size" = 1;
NightlyRate = {
....
How can I get the value from total key. Neither I can apply valueForKey nor I can apply objectForKey.
Any help is highly appreciated !
It looks strange that you have "#total" instead of "total" - looks like # is a part of NSString.
Probably you created a NSString #"#total" which sounds like a bug.
You should be able to access it with:
[dict objectForKey:#"#total"];
You can use this.
float total = [[yourDict objectForKey:#"#total"] floatValue];
Hey Buddy always use like this ........
with this line you get all value of ABC Key In your Array
[array addObject:[dict objectForKey:#"ABC"]];
Nowerdays, you could even use
yourDict[#"yourkey"]
to get the value you stored for yourkey.

Accessing NSDictionary retrieves an object instead of NSString

I have an NSDictionary that I fill with JSON. It all looks great, and it is partially working for me. I am setting a property of my class that is of type NSString equal to a value for a certain key, which oddly enough sets my property equal to an object with the object being the string value I need.
The dictionary (printed description):
miscInfo:
<__NSArrayI 0x7494ab0>(
{
Abbreviation = DR;
DateInactive = "";
Description = Drowsiness;
ForQueue = 2430;
IsAdditive = 0;
IsReserved = 1;
MiscCodeTypeStr = AVR;
PluralDescription = "";
ReservedDescription = Drowsiness;
}
I am setting it on my property like so:
self.ReactionName = [miscInfo valueForKeyPath:#"Description"];
Which produces:
I've tried casting it as NSString such as:
self.ReactionName = (NSString*)[allergenAdverseReaction valueForKeyPath:#"Description"];
but nothing gives. What am I doing wrong?
miscInfo is an array containing a dictionary, so
self.ReactionName = [[miscInfo objectAtIndex:0] objectForKey:#"Description"];
would give the result that you expect. Or, using the modern array and dictionary
subscripting syntax:
self.ReactionName = miscInfo[0][#"Description"];
Remark: In your code
self.ReactionName = [miscInfo valueForKeyPath:#"Description"];
valueForKeyPath is applied to each element of the array, and an array with all
the values is returned. That is what you see in the Xcode debugger window.
This "feature" can be very useful, but in general (as Hot Licks commented above)
objectForKey is the right method to get a value from a dictionary. And
self.ReactionName = [miscInfo objectForKey:#"Description"];
throws a descriptive runtime exception if miscInfo is not a dictionary as expected.
It looks to me like miscInfo is an array of dictionaries. In that case valueForKey will return an array. See if the code below works. If it does, then it is an array of dictionaries.
self.ReactionName = [[allergenAdverseReaction valueForKeyPath:#"Description"]lastObject];

ios assigning value to a string from an array

So I have a basic array:
NSMutableArray *answerButtonsArrayWithURL = [NSMutableArray arrayWithObjects:self.playView.coverURL1, self.playView.coverURL2, self.playView.coverURL3, self.playView.coverURL4, nil];
The objects inside are strings. I want to access a random object from that array
int rndValueForURLS = arc4random() % 3;
and assigning it a value. I've tried manny different approaches but my recent one is
[[answerButtonsArrayWithURL objectAtIndex:rndValueForURLS] stringByAppendingString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];
Any help will be much appreciated. Thanks
You need to assign it. You're already building the new value like that:
NSString *oldValue = answerButtonsArrayWithURL[rndValueForURLS];
NSString *newValue = [oldValue stringByAppendingString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];
The part you're missing :
answerButtonsArrayWithURL[rndValueForURLS] = newValue;
Above would be the way to replace the immutable string with another. If the strings are mutable, that is, they were created as NSMutableString, you could do:
NSMutableString *value = answerButtonsArrayWithURL[rndValueForURLS];
[value appendString:[self.coverFromRightAnswer objectAtIndex:self.rndValueForQuestions]];
Note:
Everywhere I replace the notation :
[answerButtonsArrayWithURL objectAtIndex:rndValueForURLS];
with the new equivalent and IMO more readable:
answerButtonsArrayWithURL[rndValueForURLS];

Resources