I have this file below:
I have a question, how can I get url value from this plist file.
I have convert this file to a NSDictionary and it has all keys and values.
I've added this line to check how it works
NSLog(#"%#", [[[[source objectForKey:#"Root"] objectForKey:#"updates"] objectForKey:#"Item 0"] objectForKey:#"url"]);
but it returns (null) instead of someurl1.
source - this is my NSDictionary instance variable.
This log for my NSDictionary object
(NSDictionary *) $1 = 0x0ab38b60 {
plist = {
dict = {
array = {
dict = (
{
integer = 4;
key = (
id,
url,
compression
);
string = "http://someurl1";
true = "";
},
{
integer = 5;
key = (
id,
url,
compression
);
string = "http://someurl2";
true = "";
}
);
};
key = updates;
};
version = "1.0";
};
}
Try:
for (NSDictionary *item in [source objectForKey:#"updates"]) {
NSLog(#"%#", [item objectForKey:#"url"]);
}
Note that 'Root' isn't a key, it's just the way Xcode displays the plist. And you need to loop over an array, again 'Item 0' is just the way it's displayed in the IDE.
Very well.
Try this code.
NSDictionary *rootDictionary = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"PListFile" ofType:#"plist"]];
NSLog(#"%#", [[[rootDictionary objectForKey:#"updates"] objectAtIndex:0] objectForKey:#"url"]);
Related
I want to typecast or change object type.
Here dict is NSDictionary
NSLog(#"%#", dict);
I'm getting this output : It is of Type: NSObject
{
data1 = (
{
lang = en;
value = "ABC";
},
{
lang = en;
value = "ABC";
}
);
}
But I want it should be like this inside this parenthesis ( )
It is of Type: NSDictionary
(
{
data1 = (
{
lang = en;
value = "ABC";
},
{
lang = es;
value = "XYZ";
}
);
}
)
Please help me I'm new to it.
Try this code to add your dictionary into array
NSDictionary *data1 = #{#"data1":#[#{#"lang":#"en",#"value":#"ABC"},#{#"lang":#"en",#"value":#"ABC"}]};
NSArray *arr = [NSArray arrayWithObject:data1];
NSLog(#"%#",arr);
Try this:
NSArray *arr = [NSArray arrayWithObject: dict];
NSLog(#"%#",arr);
Hope that solves your problem. :)
create an array.put the dictionary in the array and print it.you will get the output you want.
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:dict, nil];
NSLog(#"%#",array);
You can also do the same thing like this
NSArray *array = #[#{#"name":#"abc",#"class":#"efg",}];
NSLog(#"array - %#",array);
NSArray *arrayOfDictionary = [NSArray arrayWithObject:dict];
NSLog(#"%#",arrayOfDictionary);
this will work.
I have an NSDictionary with the following data:
(lldb) po allFriends
{
71685207018702188 = {
id = 71685207018702188;
name = "mikeziri ";
username = mi;
};
93374822540641772 = {
id = 93374822540641772;
name = "Alan Weclipse";
username = zuka;
};
96553685978449395 = {
id = 96553685978449395;
name = "Monica Weclipse";
username = amonica;
};
96556113096345076 = {
id = 96556113096345076;
name = Xavier;
username = branko;
};
97017008427632119 = {
id = 97017008427632119;
name = "Dario Weclipse";
username = tarzan;
};
}
I'm sorting these objects based on the name, if they don't have a name, i will use the username. To do that, i create a new NSDictionary with the name and id and at the end of the method i sort them by name. The code to sort them is the following:
- (NSArray*)orderFriends
{
NSMutableDictionary* newFriendsDict = [[NSMutableDictionary alloc] init];
for (int i=0; i<[allFriends count];i++)
{
NSMutableDictionary* friendsDict = [[NSMutableDictionary alloc] init];
NSDictionary* friend = [allFriends objectForKey:[NSString stringWithFormat:#"%#", [sortedKeysFriends objectAtIndex:i]]];
if ([[friend objectForKey:#"name"] length] != 0)
{
[friendsDict setObject:[friend objectForKey:#"id"] forKey:#"id"];
[friendsDict setObject:[NSString stringWithFormat:#"%#", [friend objectForKey:#"name"]] forKey:#"name"];
}
else
{
[friendsDict setObject:[friend objectForKey:#"id"] forKey:#"id"];
[friendsDict setObject:[NSString stringWithFormat:#"%#", [friend objectForKey:#"username"]] forKey:#"name"];
}
[newFriendsDict setObject:friendsDict forKey:[NSNumber numberWithInt:i]];
}
NSArray* sp = nil;
sp = [[newFriendsDict allValues] sortedArrayUsingComparator:^(id obj1, id obj2){
NSString *one = [NSString stringWithFormat:#"%#", [obj1 objectForKey:#"name"]];
NSString *two = [NSString stringWithFormat:#"%#", [obj2 objectForKey:#"name"]];
return [one compare:two];
}];
return sp;
}
The problem is that the end result is wrong:
(lldb) po sp
<__NSArrayI 0x160491a0>(
{
id = 93374822540641772;
name = "Alan Weclipse";
},
{
id = 97017008427632119;
name = "Dario Weclipse";
},
{
id = 96553685978449395;
name = "Monica Weclipse";
},
{
id = 96556113096345076;
name = Xavier;
},
{
id = 71685207018702188;
name = "mikeziri ";
},
)
Case sensitive. make all string small or big.
You could also just change
return [one compare:two];
to
return [one compare:two options: options:NSCaseInsensitiveSearch];
Than it will be ordered alphabetically, no matter if upper or lower case...
Several things: There is no reason to build different dictionaries in order to sort, and good reason NOT to do so.
You already found the method sortedArrayUsingComparator. That takes a block that is used to compare pairs of objects, and returns a sorted array. You can use that method to implement any sorting criteria you want.
I would suggest writing a comparator block that compares the name properties of your objects unless it's blank, and uses username if that's blank. It would only be a few lines of code:
NSArray *sortedFriends = [[allFriends allValues] sortedArrayUsingComparator:
^(NSDictionary *obj1, NSDictionary *obj2)
{
NSString* key1 = obj1[#"name"] ? obj1[#"name"] : obj1[#"username"];
NSString* key2 = obj2[#"name"] ? obj2[#"name"] : obj2[#"username"];
return [key1 caseInsensitiveCompare: key2];
}];
EDIT: I just noticed (from your edit of my post) that you are starting from a dictionary, not an array. So what you want to do is to create a sorted array of all the values in the dictionary? Is it acceptable to discard the keys for all the items in your dictionary, and end up with a sorted array of the values?
The other thing you could do would be to build an array of the dictionary keys, sorted based on your sort criteria. Then you could use the array of keys to fetch the items from your dictionary in sorted order.
I want to give a key value from my NSDictionary and get the value associated to it.
I have this:
NSArray *plistContent = [NSArray arrayWithContentsOfURL:file];
NSLog(#"array::%#", plistContent);
dict = [plistContent objectAtIndex:indexPath.row];
cell.textLabel.text = [dict objectForKey:#"code"];
with plistContent :
(
{
code = world;
key = hello;
},
{
code = 456;
key = 123;
},
{
code = 1;
key = yes;
}
)
So how do I get "hello" by giving the dictionary "world"?
If I understand your question correctly, you want to locate the dictionary where "code" = "world" in order to get the value for "key".
If you want to keep the data structure as it is, then you will have to perform a sequential search, and one way to do that is simply:
NSString *keyValue = nil;
NSString *searchCode = #"world";
for (NSDictionary *dict in plistContents) {
if ([[dict objectForKey:#"code"] isEqualToString:searchCode]) {
keyValue = [dict objectForKey:#"key"]); // found it!
break;
}
}
However if you do alot of this searching then you are better off re-organizing the data structure so that it's a dictionary of dictionaries, keyed on the "code" value, converting it like this:
NSMutableDictionary *dictOfDicts = [[NSMutableDictionary alloc] init];
for (NSDictionary *dict in plistContents) {
[dictOfDicts setObject:dict
forKey:[dict objectForKey:#"code"]];
}
(note that code will break if one of the dictionaries doesn't contain the "code" entry).
And then look-up is as simple as:
NSDictionary *dict = [dictOfDicts objectForKey:#"world"]);
This will be "dead quick".
- (NSString*) findValueByCode:(NSString*) code inArray:(NSArray*) arr
{
for(NSDictonary* dict in arr)
{
if([[dict valueForKey:#"code"] isEqualToString:code])
{
return [dict valueForKey:#"key"]
}
}
return nil;
}
I am using this xmlreader. Here is my code
NSDictionary *xmlDict = [XMLReader dictionaryForXMLString:responseString error:&error1];
NSLog(#"XMLData: %#",xmlDict);
I can save and log the data and it looks like this.
response = {
Gpn0 = {
text = 10000;
};
Gsn0 = {
text = 4;
};
btn0 = {
text = up;
};
};
}
But how can I access a single element from this dictionary?
NSDictionary *gpn0 = response[#"Gpn0"];
NSNumber *gpn0_text = gpno[#"text"]; // note this is a numeric value
NSDictionary *btn0 = response[#"btn0"];
NSString *btn0_text = gpno[#"text"]; // note this is a string value
so on and so forth
I have a view that has tableviewcells on it, loaded with different "key values" as the label. When I tap on one I open another view. However here, I pass the dictionary for just that key, for example I would pass this:
{
key = Budget;
value = {
"2012 Budget Report" = {
active = 0;
author = "xxxxx xxxxxx";
date = "October 27, 2012";
description = "Example";
dl = "53 downloads";
email = "xxx#xxxxx.com";
ext = DOCX;
fortest = "Tuesday, November 6";
id = 5;
subject = budget;
testdate = "Tuesday, November 6";
title = "Budget spreadSheet";
};
"2005 - 2008 Budget Report" = {
active = 0;
author = "xxxxxxx xxxxx";
date = "November 3, 2012";
description = "Example";
dl = "18 downloads";
email = "xxxxx#xxxxx.com";
ext = DOCX;
title = "Budget report";
};
};
}
How do I get each of these values? Thanks.
Please note: the titles in value array are subject to change... More could be added, one could be deleted, so I need a general solution.
Considering the dictionary you passed is saved in iDictionary.
NSDictionary *iDictionary // Input Dictionary;
NSDictionary *theValues = [NSDictionary dictionaryWithDictionary:[iDictionary valueForKey:#"value"]];
for (NSString *aKey in [theValues allKeys]) {
NSDictionary *aValue = [theValues valueForKey:aKey];
NSLog(#"Key : %#", aKey);
NSLog(#"Value : %#", aValue);
// Extract individual values
NSLog(#"Author : %#", [aValue objectForKey:#"author"]);
// If the titles are dynamic
for (NSString *aSubKey in [aValue allKeys]) {
NSString *aSubValue = [aValue objectForKey:aSubKey];
NSLog(#"SubKey : %#, SubValue = %#", aSubKey, aSubValue);
}
}
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSArray *arrBudget= [jsonDictionary objectForKey:#"Budget"];
So here arrBudget will contain All the values And you can Pass the array to detail view.
Another approach if keys and objects are useful in the "foreach" logic :
NSDictionary *dict = #{
#"key1": #"value1",
#"key2": #"value2",
#"key3": #"value3",
};
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(#"Your key : %#", key);
NSLog(#"Your value : %#", [obj description]);
// do something...
}];