0

I have an NSMutableDictionary with a structure like:

Main Dictionary > Unknown Dictionary > Dictionaries 1,2,4,5,6...

My question is what is the best way to retrieve the Unknown Dictionary key and set it as a variable? This is what I've tried:

NSEnumerator *enumerator = [myMutableDict keyEnumerator];
id aKey = nil;
while ( (aKey = [enumerator nextObject]) != nil) {
    id value = [myMutableDict objectForKey:aKey]; // changed to `aKey`
    NSLog(@"%@: %@", aKey, value);                // tip via rmaddy
} 

What goes into objectForKey: if you don't know the name of the object in the key?

The other thought I had was to populate an NSArray, then pulling each of the keys out somehow.

for (NSString *object in myMutableDict)
        myArray = [myArray arrayByAddingObject:MainDict];
}

If anyone can suggest a better way to get the object (unknown) from an NSMutableDictionary I'm interested to learn.

nobody
  • 19,814
  • 17
  • 56
  • 77
ctfd
  • 338
  • 3
  • 14
  • Your for loop works fine to let you iterate through all the keys in `myMutableDict`. What's the problem you're asking about? – mah May 14 '14 at 00:17
  • @mah, Just wondering the best and most efficient way. Also I'm not sure how the keyEnumerator works exactly. I put a comment in the code I was confused with. – ctfd May 14 '14 at 00:18
  • The call to `objectForKey:` just inside the `while` loop should be `[myMuyableDict objectForKey:aKey]`. – rmaddy May 14 '14 at 00:18
  • In all likelihood the efficiency will be close enough for what you're doing, but if you're not sure, you should really run some timing experiments that model _your_ runtime environment as best as you can. – mah May 14 '14 at 00:19
  • @rmaddy. Ah, okay. so does it do objects and keys or just keys? I got that code snippet from here: http://stackoverflow.com/a/1062159/3257552 – ctfd May 14 '14 at 00:20
  • 1
    @ctfd You are enumerating the keys. You use those keys to get the values. – rmaddy May 14 '14 at 00:28
  • @rmaddy, yes the enumeration works very well, thanks for the tip. Why would you use that over a for loop? any particular reason, that's mainly what I'm wondering. – ctfd May 14 '14 at 00:32

2 Answers2

1

You can enumerate dictionaries like this:

NSDictionary * someDictionary = ... however you set your dictionary;
[someDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"Key: %@", key);
    NSLog(@"Object: %@", obj);


}];

and set:

*stop = YES; 

when you find the object you're looking for.

Logan
  • 52,262
  • 20
  • 99
  • 128
  • Are blocks more efficient than the older style syntax, and are there any caveats in using blocks (ARC specific, etc.)? thanks! – ctfd May 14 '14 at 00:35
  • I don't know if they're more efficient. Caveats - You might have to be a bit more aware of how you're assigning things. This is mostly relevant for background threads, or when a viewcontroller is referenced in a block that is retained by said viewcontroller. Using blocks like this really shouldn't cause any problems. If you want to use local variables, you'll have to put a `__block` specifier. On a personal note, I absolutely LOVE blocks. They're immensely useful! – Logan May 14 '14 at 00:38
  • I personally prefer this solution because it gets me all the objects and keys directly, so it saves me a step or two. – Logan May 14 '14 at 00:40
  • Hi Logan, thanks for the reply - I've used some __block specifiers actually for some of my filepaths within a dispatch queue, but I've barely scratched the surface with it. – ctfd May 14 '14 at 01:47
0

I'm not entirely sure if I understand your question correctly. I assume you have a "main" dictionary with exactly one (unknown) key that maps to another dictionary, which you want to retrieve. This would be a simple and concise way to do this:

NSDictionary *unknownDictionary = mainDictionary[mainDictionary.allKeys.firstObject];

(Yes, some people won't like the dot syntax here, but I find it easier to read in this case. You might also want to add some error checking, for the case that mainDictionary is empty etc.)

omz
  • 53,243
  • 5
  • 129
  • 141
  • thank you for the example. you're correct - i just want to find the best way of getting an unknown value from my dictionary. I've been have some issues with comparing the values with `lastPathComponent` crashing because of unknown selectors. I'm hoping by knowing the keys in advance I can avoid those issues. – ctfd May 14 '14 at 01:42