How to read dictionary with child - iphone

Help me in reading this dictionary: (its an NSDictionary generated from XMLData)
Menus = {
Table1 = {
Phone = {
text = "\n (404) 371-1466";
};
text = "\n ";
};
text = "\n";
};
}

This should get you the phone number, for example:
NSString *phoneNum = [[[[dict objectForKey:#"Menus"] objectForKey:#"Table1"]
objectForKey:#"Phone"] objectForKey:#"text"];

You have one item in the dictionary - "Menus".
That single item is a dictionary, with two items - "Table 1" and "text". "Table 1" is a dictionary, "text" is an NSString.
Inside "Table 1", you have two items - "Phone" and "text". At this point, I think you can see the pattern...

Related

How to access Delete Row function in a Multivalued row for a Eureka form

I would like to access the delete row function in the eureka form but I couldn't find how to do it. The reason is that I have an i variable that keeps track of how many rows I have and because of that my form breaks when a row is deleted.
This is how my implementation currently looks in my code:
+++ MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete], header: "Exercises", footer: "List of Exercises for workout"){
$0.addButtonProvider = {section in
return ButtonRow(){
$0.title = "Add Another Exercise"
self.i+=1
}
}
$0.multivaluedRowToInsertAt = { index in
return TextRow() {
$0.title = "Exercise \(self.i) Name"
$0.tag = "Exercise \(self.i) Name"
self.i+=1
}
}
$0 <<< TextRow(){ //change this to SuggestionRow later
$0.title = "Exercise \(self.i) Name"
$0.tag = "Exercise \(self.i) Name"
$0.add(ruleSet: strictRules)
}
}
What I'm looking for is something like:
$0.multivaluedRowToDeleteAt = { index in
self.i-=1
}
Does something like this exist? If not, is there any other way to get the number of rows for this multivalued section at submission time?

Iterate over elements in hash and updating its values

At the beginning I set all hash values to null.
When a name in the hash is equal to a certain name the hash value should be set to 1.
However, I get an error
collection was modified, enumeration may not execute
Which is really stupid, since I do not change the values for the keys. Any ideas?
$Fieldlist = #{
"Brand Name" = 0;
"Product Type" = 0
}
# Loops thru an array
$ArrayOfNames = ("Name1", "Name2", ... ,"Brand Name" ...)
foreach ($FieldName in $Fieldlist.Keys) {
$Message = $FieldName + " -eq " + $ColName + " ?"
Write-Host $Message
if ($FieldName -eq $ColName) {
Write-Host "-------> Yes, bingo"
$Fieldlist.$FieldName = 1
}
}
Reverse your logic. Instead of iterating over the keys of the hashtable and comparing them to a value from $ArrayOfNames (I suppose that's where $ColName is coming from) iterate over the name array and check if the hashtable contains that key before changing the value:
foreach ($FieldName in $ArrayOfNames) {
if ($Fieldlist.ContainsKey($FieldName)) {
$Fieldlist.$FieldName = 1
}
}

Sort/group NSDictionary by key

i have data in NSDictionary like below :
Value : Football, Key: SPORT
Value : Cricket, Key: SPORT
Value : Fastrack, Key: PRODUCT/SERVICE
Value : Audi USA, Key: CARS
Value : BMW, Key: CARS
Value : Facebook, Key: PRODUCT/SERVICE
Value : TED, Key: WEBSITE
Value : National Geographic, Key: MEDIA/NEWS/PUBLISHING
Value : MyWebProduct, Key: WEBSITE
i want grouping of values according to key. what i need to do in this case or another suitable idea to implement this. I want result to be display something like :
SPORT : Football, Cricket
CARS : Audi, BMW
...
any help appreciable ...
Since you have multiple objects grouped under the same key, a dictionary of arrays would be a suitable structure to contain your data.
NSMutableDictionary *dict = [ [ NSMutableDictionary alloc ] init ];
NSArray *myItems = [ [ NSArray alloc ] initWithObjects:#"item one", #"item two", nil ];
[ dict setObject:myItems forKey:#"group of items" ];
Then you can access the group using
[dict objectForKey:#"group of items"]
Would this work for you, as a category on NSMutableDictionary:
- (void)setObject:(id)object inArrayForKey:(id <NSCopying>)key
{
id current = [self objectForKey:key];
if (!current || ![current isKindOfClass:[NSArray class]]) {
[self setObject:#[object] forKey:key];
} else {
[self setObject:[(NSArray *)current arrayByAddingObject:object] forKey:key];
}
}
Basically, you would then have a clean interface adding an item into an array associated with the key. You could choose to only make it an array if there was more than one item, if that's your preference.
If you're not familiar with adding categories, they allow you to add methods to existing classes. In Xcode just do new file > objective-C category and add the category on NSMutableDictionary.
NSDictionary cannot be sorted by default. If you want sorted dictionary, go on and implement your own subclass of it (this is one of the few valid reasons for which you can subclass a standard container object).

Parsing JSON: brackets in response

I want to parse this JSON YouTube recently featured.
This is how I get the title of the videos:
NSString *response = [request responseString];
responseDict = [response JSONValue];
videoTitleArray = [responseDict valueForKeyPath:#"feed.entry.title.$t"];
And it works like I want it.
But I also want to display the author of the video.
But the following code for this does not work properly:
videoAuthorArray = [responseDict valueForKeyPath:#"feed.entry.author.name.$t"];
NSLog(#"%#", videoAuthorArray);
The list of the authors I get looks like this:
(
author 1
),
(
author 2
),
(
author 3
),
(
author 4
),
I can't display the names in for example a table view because of the brackets.
How can I display the author names like the video titles?
When you see an Author you see this:
"author":[{"name":{"$t":"mls"},"uri":{"$t":"http://gdata.youtube.com/feeds/api/users/mls"},"yt$userId":{"$t":"SZbXT5TLLW_i-5W8FZpFsg"}}]
this means: author is an array of author objects
each author object has: name object, a uri object and a yt$userId object
each of this objects described above is a NSDictionary
formated we have:
"author":[
{
"name": {
"$t":"mls"
},
"uri": {
"$t":"http://gdata.youtube.com/feeds/api/users/mls"
},
"yt$userId":{
"$t":"SZbXT5TLLW_i-5W8FZpFsg"
}
}
],
so if you have your videoAuthorArray each element is an NSDictionary and has this keys: name, uri and yt$userId
each of this objects has an NSDictionary with a single key: $t witch has the value

How to represent lists of blocks in Configgy?

Configgy supports lists of strings as values and blocks of key/value pairs.
But it seems it does not support lists of blocks of key/value pairs as values.
Am I missing something?
I ended up using nested blocks:
contacts {
c1 {
name = "Joe Smith"
email = "jsmith#example.com"
}
c2 {
name = "John Brown"
email = "jbrown#example.com"
}
}