Gold challenge


#1

I thought I figured this challenge out, but after looking at the solutions that other people came up with, I feel like I’m missing something… all I did was saved the title, and had the dictionary hold the array. Then the BNRFeedStore can save the dictionary that is returned from this method. Arrays are serializable, so I did not see any reason to add each item individually to this dictionary, when the array itself would (seems to ?) suffice.

- (NSDictionary *)preserveObjectInDictionary {
    NSLog(@"Preserving RSSChannel...");

    NSDictionary *dict = @{
                           @"title" : [self title],
                           @"items" : [self items]
                           };
        
    return dict;
}

Thoughts?


#2

But the more I think about it, I don’t see why this code works. It’s not reading the same type of dictionary that it’s getting from the server.

?


#3

For anyone else who was thinking the same thing I was by trying this… the above “solution” actually did not work, but appeared to for some reason. What I ended up doing was drawing out the relationships between the JSON objects on paper, and started at the bottom and worked my way up when rebuilding the dictionaries from my picture. My solution turned out to be almost exactly the same as other people who have posted their solutions:

RSSItem’s implementation of the new delegate method:

/*
 * Reconstruct the JSONData exactly as it comes in from the server
 * I looked at the raw JSON data and diagrammed out the relationships that
 * this application requires on paper.
 *
 * use this JSON data:
 * http://itunes.apple.com/us/rss/topsongs/limit=5/json
 * and copy it into www.jsonprettyprint.com
 */
- (NSDictionary *)writeToJSONDictionary {
    NSDictionary *hrefDict = @{ @"href": [self link] };
    NSDictionary *attributesDictionary = @{ @"attributes" : hrefDict };
    NSArray *links = @[ [NSNull null], attributesDictionary ];
    
    NSDictionary *labelDict = @{ @"label" : [self title] };
    
    NSDictionary *topLevelRSSItemDict = @{
                                          @"title" : labelDict,
                                          @"link": links
                                          };
    
    return topLevelRSSItemDict;
}

RSSChannel’s implementation of the new delegate method:

- (NSDictionary *)writeToJSONDictionary {
    NSDictionary *labelDict = @{ @"label" : [self title] };
    NSDictionary *infoDict = @{ @"label" : [self infoString] };
    
    NSMutableArray *entries = [NSMutableArray array];
    for (RSSItem *i in [self items]) {
        NSDictionary *entryDict = [i writeToJSONDictionary];
        [entries addObject:entryDict];
    }
    
    NSDictionary *feedDict = @{
                               @"entry" : entries,
                               @"title" : labelDict,
                               @"rights" : infoDict
                               };
    
    NSDictionary *topLevelFeedDict = @{ @"feed" : feedDict };
    
    return topLevelFeedDict;
}