How to get value from dictionary - ios

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.

Related

objc How do I get the string object from this Json array?

This is part of an incoming array:
variantArray: (
(
{
CardinalDirection = "North-West";
DirectionVariantId = "DcCi_1445_171_0_0";
Distance = "2.516606318971459";
RouteName = "Woodsy";
Shape = {
Points = (
{
I want to get the value of DirectionVariantId
I would normally loop and use
NSMutableArray *myString = [variantArray[i] valueForKey:#"DirectionVariantId"];
This isn't working and results in an exception when I try to examine the last character in the string:
NSString *lastChar = [myString substringFromIndex:[myString length] - 1];
This is a new data set for me and I'm missing something..
Thanks for any tips.
Json contain two curly bracket means nested array.
Try:
NSString *myString=[[[variantArray objectAtIndex:0] objectAtIndex:0] objectForKey:#"DirectionVariantId"];
I think you're looking for [variantArray[i] objectForKey:#"DirectionVariantId"];
You'd need to convert the object within your incoming array (variantArray[i]) to a NSDictionary but it might already be judging by your original output.

How to get values from nested array/ dictionary iOS 7

I fetch values from dictionary and need to display in UITableView, but everything works fine.
On some spot it stops running and shows thread
-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0xbfa7670
The code below, which I used to fetch value..
[NSString stringWithFormat:#"%#",[[pageCat1 valueForKeyPath:#"img3"] objectAtIndex:indexPath.row]]
My values are fetched properly in dictionary but lags to display it?
pageCat (
{
img3 = "http://xxx.in/images/page_cat_img/75x75/4.jpg";
name = "PVC Flexible Wires";
page = (
{
id = {
text = 1;
};
img4 = "http://xxxx.in/images/page_img/75x75/1.jpg";
name = "SINGLE CORE FLEXIBLE WIRES ABOVE 6 SQMM";
},
{
id = {
text = 72;
};
img4 = "http://xxx.in/images/page_img/75x75/72.jpg";
name = "SINGLE CORE FLEXIBLE WIRES BELOW 6 SQMM";
}
);
},
{
img3 = "http://xxx.in/images/page_cat_img/75x75/3.jpg";
name = "Bare Copper Wires";
page = {
id = {
text = 29;
};
img4 = "http://xxx.in/images/page_img/75x75/29.jpg";
name = "Tinned Copper Fuse Wires";
};
},
{
img3 = "http://xxx.in/images/page_cat_img/75x75/48.jpg";
name = "Properties of Wire";
page = {
id = {
text = 85;
};
img4 = "http://xxx.in/images/page_img/75x75/85.jpg";
name = "Wires - Normal, HR - PVC, FR, FRLS & Zero Halogen";
};
}
)
Actually look at the log value, it has array and set of values.. i can't find whether it is in what form..
Can anyone help me to find the solution??
Thanks,
Yazh
it looks like [pageCat1 valueForKeyPath:#"img3"] returns a NSString and not a NSArray like you expect
make sure that it returns a NSArray before applying objectAtIndex:
it seems that pageCat1 is a NSArray so you need to write something like:
NSString *path = pageCat1[0][#"img3"];
...
As the error already tells, [pageCat1 valueForKeyPath:#"img3"] returns a NSString and you are calling objectAtIndex: on it which is not recognized for this class. Obviously, pageCat1 differs from what you expected.
Try NSLog(#"%#", pageCat1); to see what it really looks like.
// Edit
pageCat1 (as seen in your update) is an NSArray that contains items of type NSDictionary. What you really want to do is NSString *imgURL = [[pageCat1 objectAtIndex:indexPath.row] objectForKey:#"img3"];
Explanation:
1. [pageCat1 objectAtIndex:indexPath.row] returns a NSDictionary
2. [__dictionary__ objectForKey:#"img3"] returns the NSString containing your image URL
Actually I used XML data, for that i used third party to parse data. Its all of third party which parsed alternate data as array and other as non-array. Finally I check the array with
isKindOfClass
and convert it into array. Therefore my problem in app solved. :-)
Thanks to all who help me..
Please try this one:
//Assuming json is your main dictionary
NSDictionary *pageCat = [json objectForKey:#"pageCat"];
NSMutableArray *array = [[pageCat valueForKey:#"img3"]mutableCopy];
NSLog(#"Value=%#", [array objectAtIndex:indexPath.row]);

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];

NSDictionary objectForKey returns wrong value

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"];

Resources