Graphics Programs Reference
In-Depth Information
[self completionBlock](rootObject, nil);
[sharedConnectionList removeObject:self];
}
In
BNRFeedStore.m
, change the method
fetchTopSongs:withCompletion:
so that it supplies a
jsonRootObject
to the connection instead of an
xmlRootOb-
ject
.
BNRConnection *connection = [[BNRConnection alloc] initWithRequest:req];
[connection setCompletionBlock:block];
[connection setXmlRootObject:channel];
[connection setJsonRootObject:channel];
[connection start];
You can build the application to check for syntax errors. You will see one warning in
BNRFeedStore.m
that says
RSSChannel
is incompatible with
setJsonRootOb-
ject:
. That's because
RSSChannel
doesn't yet conform to
JSONSerializable
.
In
RSSChannel.h
, import this file and declare that
RSSChannel
conforms to this pro-
tocol.
#import "JSONSerializable.h"
@interface RSSChannel : NSObject <NSXMLParserDelegate
, JSONSerializable
>
Now that
RSSChannel
and
RSSItem
conform to
JSONSerializable
, once the
connection completes, they will pull their data from the dictionary when sent
readFromJSONDictionary:
.
The implementation of
readFromJSONDictionary:
should take values from the
dictionary argument and move them into the instance variables of the
RSSChannel
. The
channel needs to grab the title and all of the entries from the channel. In
RSSChan-
nel.m
, implement this method to do just that.
- (void)readFromJSONDictionary:(NSDictionary *)d
{
// The top-level object contains a "feed" object, which is the channel.
NSDictionary *feed = [d objectForKey:@"feed"];
// The feed has a title property, make this the title of our channel.
[self setTitle:[feed objectForKey:@"title"]];
// The feed also has an array of entries, for each one, make a new RSSItem.
NSArray *entries = [feed objectForKey:@"entry"];
for (NSDictionary *entry in entries) {
RSSItem *i = [[RSSItem alloc] init];
// Pass the entry dictionary to the item so it can grab its ivars
[i readFromJSONDictionary:entry];
[items addObject:i];
}