Search code examples
iphonekeynsdictionarynsmutabledictionary

How do I access the keys of a NSMutableDictionary?


NSMutableDictionary *bookmarks = [NSMutableDictionary dictionary];

[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://stanford.edu"] forKey:
@"Stanford University"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://apple.com"] forKey:
@"Apple"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://itunes.stanford.edu"] forKey:
@"Stanford on iTunesU"];
[bookmarks setObject:[NSURL URLWithString:(NSString *) @"http://stanfordshop.com"] forKey:
@"Stanford Mall"];

NSEnumerator *browser = [bookmarks keyEnumerator];
id each;
NSURL *url;
while ((each = [browser nextObject])) {
url = [bookmarks objectForKey:(NSString *)each];
NSLog('%@'", url);

I realize Stanford's iPhone programming stuff has been beat to death (I'm sure). I'm just having a little trouble finding out how I would print the keys with the URLs. I also can't figure out how to only print keys that start with Stanford. I know it's a method of NSString though.


Solution

  • for (NSString * key in [bookmarks allKeys])
    {
      if ([key hasPrefix:@"Stanford"])
      {
        NSLog(key);
      }
    }