Not sure of the value being retrieved from NSDictionary - iphone

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.

Related

Swift, dictionary parse error

I'm using an API to get weather condition and the retrieved dict is
dict = {
base = stations;
clouds = {
all = 92;
};
cod = 200;
coord = {
lat = "31.23";
lon = "121.47";
};
dt = 1476853699;
id = 1796231;
main = {
"grnd_level" = "1028.63";
humidity = 93;
pressure = "1028.63";
"sea_level" = "1029.5";
temp = "73.38";
"temp_max" = "73.38";
"temp_min" = "73.38";
};
name = "Shanghai Shi";
rain = {
3h = "0.665";
};
sys = {
country = CN;
message = "0.0125";
sunrise = 1476827992;
sunset = 1476868662;
};
weather = (
{
description = "light rain";
icon = 10d;
id = 500;
main = Rain;
}
);
wind = {
deg = "84.50239999999999";
speed = "5.97";
};
}
If I want the value of humidity, I just use
let humidityValue = dict["main"]["humidity"] and it works.
But the problem is I also want to get the value of description in weather
when I used let dscptValue = dict["weather"]["description"]
it retrieved nil.
How's that? and I notice there are two brackets around weather .I'm not sure whether it is the same with the statement without brackets.
weather = (
{
description = "light rain";
icon = 10d;
id = 500;
main = Rain;
}
);
How to get the value of description?
weather keys contains Array of Dictionary not directly Dictionary, so you need to access the first object of it.
if let weather = dict["weather"] as? [[String: AnyObject]], let weatherDict = weather.first {
let dscptValue = weatherDict["description"]
}
Note: I have used optional wrapping with if let for preventing crash with forced wrapping.
Weather is an array of dictionaries.
dict["weather"][0]["description"]
may give you the expected result.

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

Cannot getting an Array from NSMutableDictionary

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

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

Filter array using predicate

I need to filter following array with status equal to "U" and i have used following.
NSArray *result = [alertModified.senders filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(subscriptions.status = %#)",#"U"]];
But I'm getting empty arrays.
Please help me on filtering this?
Array String:
(senderCode = CPFB, senderName = CPFB, forSenderLevel = 0, subscriptions = (
"correspondenceListId = 102,status = S,senderCode = AA,subject = Letter,retentionPeriod = 0, uniqueBillIdentifier = (null),senderResponseStatus = (null),subscriptionDate = ,effectiveDate = ",
"correspondenceListId = 103,status = U,senderCode = BB,subject = Nomination Letters,retentionPeriod = 0, uniqueBillIdentifier = (null),senderResponseStatus = (null),subscriptionDate = ,effectiveDate = ",
"correspondenceListId = 104,status = U,senderCode = AA,subject = Yearly statements,retentionPeriod = 0, uniqueBillIdentifier = (null),senderResponseStatus = (null),subscriptionDate = ,effectiveDate = ",
"correspondenceListId = 105,status = U,senderCode = BB,subject = All Future Letters,retentionPeriod = 0, uniqueBillIdentifier = (null),senderResponseStatus = (null),subscriptionDate = ,effectiveDate = "))
In your example you should be filtering the subscription list itself, not the whole senders. You have to apply the filter to each sender. Try changing your filter line to this and check if it gives you results:
NSArray *result = [[alertModified.senders objectAtIndex:0].subscriptions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(status = %#)",#"U"]];