Cannot getting an Array from NSMutableDictionary - iphone

I have a dictionary like below:
{
76 = (
{
language = en;
optionid = 1;
response = ffgh;
}
);
74 = (
{
language = en;
optionid = 1;
response = "Herbert S.B. Baraf, MD";
}
);
75 = (
{
language = en;
optionid = 1;
response = ffgh;
}
);
73 = (
{
language = en;
optionid = 1;
response = Excellent;
}
);
}
I am not getting the key value array from my dictionary using below code:
NSMutableArray *Array=[m_MutDictAnswers objectForKey:m_strRuleQuestion ];
While logging the Array is empty and the m_strRuleQuestion is 73.
I don't know why I am getting an empty array.
In my json string m_strRuleQuestion is "73" , but when I edit the json to m_strRuleQuestion to 73, I am getting the correct array. I need to fix this issue with out editing the json string. Can any one help me.

NSMutableArray *Array=[m_MutDictAnswers objectForKey#"73"]
try this and then check what are you getting.

I thing this dictionary is in array first you get array may its index of 0 after that fetch dictionary array
NSString *string=[[[myarray objectAtIndex:0]objectForKey:#"72"]objectForKey:#"language"];

Related

UITableView+YouTubeAPI parsing JSON response for statistics

I am getting the JSON response from youtube API v3 for https://www.googleapis.com/youtube/v3/channels?part=statistics&id
and receiving JSON response as
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/2zirKmd0WgUqY0RzlyD4hlACeWM\"";
items = (
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/u4TmQ5XfIQQg6y6u4Od2yULCVlc\"";
id = "HCcrj0EHvn_Y8";
kind = "youtube#channel";
statistics = {
commentCount = 0;
subscriberCount = 21694;
videoCount = 124582;
viewCount = 0;
};
},
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/2p4_mjrZLfhO6bDvH-RgAykNQr8\"";
id = UCX2v47KsDKqajrEYFV7GbBg;
kind = "youtube#channel";
statistics = {
commentCount = 6460;
subscriberCount = 494656;
videoCount = 33;
viewCount = 713607227;
};
}
}
when I am trying to display the
cell.viewsChannel.text=[[_statistics valueForKeyPath:#"statistics.videoCount"] objectAtIndex:indexPath.row];
the value doesn't display on table view.
I tried to format the output as unsigned long ,int but the correct values don't show.
Please help
Thanks in advance
I think you need to add item to valueForKeyPath
cell.viewsChannel.text=[[_statistics valueForKeyPath:#"item.statistics.videoCount"] objectAtIndex:indexPath.row];

Weird value from NSDictionary

I got a NSDictionary that when I do a [currentOrder debugDescription] call on it the layout is below, when I then do a:
[currentOrder valueForKey: #"itemOrder"]
It return it funny with the brackets as:
(
"4 X ESPRESSO"
)
where it should only be (without brackets):
4 X EXPRESSO
Any idea why?
Content of currentOrder:
currentOrder: <__NSArrayM 0x68426c0>(
{
extra1Select = 0;
extra2Select = 0;
extra3Select = 0;
itemCost = 58;
itemOrder = "4 X ESPRESSO";
itemOrderDescription = "Cookie: YES, Sugar: YES";
itemQuantity = 4;
itemRestaurant = VidaECaffe;
plistItem = {
cost = "11.5";
description = "R11.50";
extra1 = {
desc = Cookie;
details = (
{
cost = "3.00";
option1 = YES;
},
{
cost = "0.00";
option2 = NO;
}
);
};
extra2 = {
desc = Sugar;
details = (
{
cost = "0.00";
option1 = YES;
},
{
cost = "0.00";
option2 = NO;
}
);
};
itemRestaurant = VidaECaffe;
level = 1;
title = ESPRESSO;
};
}
)
The problem is that currentOrder is not a dictionary but a NSArray containing a dictionary. The failure you are making next is that you use valueForKey: which is part of the key value coding family and not the designated access method for dictionaries (which is objectForKey:), and the array returns you a filtered array as a result...
When you print out the value of an array or dictionary in the debugger, the debugger includes the extra brackets, braces, and whatnot so you can see the layout of the structure you are asking about.
If the debugger shows the contents of an array something like
(
"Foobar"
)
it's telling you that the array has one element, a string with the value Foobar.
Put entirely another way: Read up on what the debugger does when it prints out values. Lots of resources here and on the web for this. For example: Debugging with GDB: Introduction to Commands, Print and Print-Object

How to get Facebook Photo Albums for iOS?

I'm making the following simple request to Facebook. My goal is to grab the names of all a user's photo albums:
[[AppDelegate sharedDelegate].facebook requestWithGraphPath:#"me/albums" andDelegate:[AppDelegate sharedDelegate]];
Then, I get a JSON response from the server like such:
data = (
{
"can_upload" = 1;
count = 4;
"cover_photo" = 321491374598618;
"created_time" = "2012-06-04T21:46:23+0000";
from = {
id = 100002132746588;
name = "Owner....";
};
id = 321491371265285;
link = "http://www.facebook.com/album.php?fbid=321491371265285&id=100002132746588&aid=73680";
name = "Photos";
privacy = custom;
type = normal;
"updated_time" = "2012-06-04T22:08:39+0000";
},
{
"can_upload" = 0;
count = 1;
"cover_photo" = 318401854907570;
"created_time" = "2012-05-31T00:00:35+0000";
description = "Which Friend Stalks You the Most?";
from = {
id = 100002132746588;
name = "Owner....";
};
id = 318401848240904;
link = "http://www.facebook.com/album.php?fbid=318401848240904&id=100002132746588&aid=73163";
name = "Which Friend Stalks You the Most?";
privacy = friends;
type = normal;
"updated_time" = "2012-05-31T00:00:36+0000";
},
And so on. Problem is, when I try to parse the data with:
NSLog(#"%#", [result objectForKey:#"name"]);
I get null. I assume this is happening because the NSDictionary that the data is returned in does not know which name entry to concentrate on. How do I parse the data so that I get the names of all the albums?
"result" is actually an array of dictionaries, so loop through the array and then grab name from each element in the array.
for (NSDictionary *anAlbum in [result objectForKey:#"data"]) {
NSLog(#"%#", [anAlbum objectForKey:#"name"]);
}

Not sure of the value being retrieved from NSDictionary

I want to retrive a value from a NSDictionary.
Here is a snippet of code:
NSDictionary *metadataDict = [representation metadata];
NSLog(#"%#",metadataDict);
"DateTimeOriginal" is the value I want to retrieve from the following output.
{
ColorModel = RGB;
DPIHeight = 72;
DPIWidth = 72;
Depth = 8;
Orientation = 6;
PixelHeight = 1936;
PixelWidth = 2592;
"{Exif}" = {
ApertureValue = "2.970854";
ColorSpace = 1;
ComponentsConfiguration = (
1,
2,
3,
0
);
DateTimeDigitized = "2011:09:28 09:35:36";
DateTimeOriginal = "2011:09:28 09:35:36";
ExifVersion = (
2,
2,
1
);
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.06666667";
FNumber = "2.8";
Flash = 24;
FlashPixVersion = (
1,
0
);
FocalLength = "3.85";
ISOSpeedRatings = (
320
);
MeteringMode = 5;
PixelXDimension = 2592;
PixelYDimension = 1936;
SceneCaptureType = 0;
SensingMethod = 2;
Sharpness = 2;
ShutterSpeedValue = "3.9112";
SubjectArea = (
1295,
967,
699,
696
);
WhiteBalance = 0;
};
"{GPS}" = {
Latitude = "37.54216666666667";
LatitudeRef = N;
Longitude = "126.95";
LongitudeRef = E;
TimeStamp = "01:19:05.00";
};
"{TIFF}" = {
DateTime = "2011:09:28 09:35:36";
Make = Apple;
Model = "iPhone 4";
Orientation = 6;
ResolutionUnit = 2;
Software = "4.3.5";
XResolution = 72;
YResolution = 72;
"_YCbCrPositioning" = 1;
};
}
I know it's long but I tried these three and it still would not work.
NSLog(#"valueForKey %#", [metadataDict valueForKey:#"DateTimeOriginal"]);
NSLog(#"valueForKeyPath %#", [metadataDict valueForKeyPath:#"DateTimeOriginal"]);
NSLog(#"objectForKey %#", [metadataDict objectForKey:#"DateTimeOriginal"]);
Does anyone know what kind of datatype is in the NSDictionary and how I can retrieve it?
Thanks much.
Above project link:
http://dl.dropbox.com/u/12439052/TheK2.zip
What you're really looking for is
NSDictionary *exif = [metadataDict objectForKey:#"{Exif}"];
NSLog(#"DateTimeOriginal: %#", [exif objectForKey:#"DateTimeOriginal"]);
If you read the output from your first log, you'll see that the key you want is actually inside another dictionary which has the key "{Exif}". Also, -objectForKey: is the better method here to use rather than -valueForKey: (the latter is for generic KVO, the former is the real dictionary object accessor).
Try this one:
NSString *DateTimeOriginal=[[metadataDict objectForKey:#"{Exif}"]objectForKey:#"DateTimeOriginal"];
During Response Value of lines keep in mind
{ .......... } indicates dictionary.
(............) indicates array.
Dictionary always have key with it like somename = "value in it".
Array has value seperated by comma like a , b , c .....
Now it is possible it may have array into dictionary like { SubjectArea = ( 1295,967,699,696);} and vice versa.

Obj-C / iPhone: NSArray question

I have an array that looks like this when printed via NSLog:
{
response = "Valid";
updates = (
{
string = "test";
integer = 3493;
},
{
string = "test2";
integer = 55454;
}
);
start-index = 0;
My question is how I can loop through through the "updates" array so that I may print the values for each "string" respectively.
Should be an easy "for" loop or something?
Dave
Assuming you NSLogged data has a type of NSDictionary with name data.
NSArray *updates = [data objectForKey:#"updates"];
for (NSDictionary *update in updates) {
NSLog(#"Update: %# - %#", [update objectForKey:#"string"], [update objectForKey:#"integer"]);
}