Learning Objective-C: Need advice on populating NSMutableDictionary

Posted by Zigrivers on Stack Overflow See other posts from Stack Overflow or by Zigrivers
Published on 2010-05-18T16:38:58Z Indexed on 2010/05/18 21:00 UTC
Read the original article Hit count: 406

Filed under:
|

I am teaching myself Objective-C utilizing a number of resources, one of which is the Stanford iPhone Dev class available via iTunes U (past 2010 class).

One of the home work assignments asked that I populate a mutable dictionary with a predefined list of keys and values (URLs). I was able to put the code together, but as I look at it, I keep thinking there is probably a much better way for me to approach what I'm trying to do:

  1. Populate a NSMutableDictionary with the predefined keys and values
  2. Enumerate through the keys of the dictionary and check each key to see if it starts with "Stanford"
  3. If it meets the criteria, log both the key and the value

I would really appreciate any feedback on how I might improve on what I've put together. I'm the very definition of a beginner, but I'm really enjoying the challenge of learning Objective-C.

void bookmarkDictionary () {
    NSMutableDictionary* bookmarks = [NSMutableDictionary dictionary];

    NSString* one = @"Stanford University",
    *two = @"Apple",
    *three = @"CS193P",
    *four = @"Stanford on iTunes U",
    *five = @"Stanford Mall";

    NSString* urlOne = @"http://www.stanford.edu",
    *urlTwo = @"http://www.apple.com",
    *urlThree = @"http://cs193p.stanford.edu",
    *urlFour = @"http://itunes.stanford.edu",
    *urlFive = @"http://stanfordshop.com";

    NSURL* oneURL = [NSURL URLWithString:urlOne];
    NSURL* twoURL = [NSURL URLWithString:urlTwo];
    NSURL* threeURL = [NSURL URLWithString:urlThree];
    NSURL* fourURL = [NSURL URLWithString:urlFour];
    NSURL* fiveURL = [NSURL URLWithString:urlFive];

    [bookmarks setObject:oneURL forKey:one];
    [bookmarks setObject:twoURL forKey:two];
    [bookmarks setObject:threeURL forKey:three];
    [bookmarks setObject:fourURL forKey:four];
    [bookmarks setObject:fiveURL forKey:five];

    NSString* akey;
    NSString* testString = @"Stanford";

    for (akey in bookmarks) {

        if ([akey hasPrefix:testString]) {
            NSLog(@"Key: %@     URL: %@", akey, [bookmarks objectForKey:akey]);
        }

    }

}

Thanks for your help!

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about learning