Graphics Programs Reference
In-Depth Information
}
Do the same thing in RSSItem.h .
#import "JSONSerializable.h"
@interface RSSItem : NSObject <NSXMLParserDelegate , JSONSerializable >
And in RSSItem.m , grab the title and link from the entry dictionary.
- (void)readFromJSONDictionary:(NSDictionary *)d
{
[self setTitle:[[d objectForKey:@"title"] objectForKey:@"label"]];
// Inside each entry is an array of links, each has an attribute object
NSArray *links = [d objectForKey:@"link"];
if ([links count] > 1) {
NSDictionary *sampleDict = [[links objectAtIndex:1]
objectForKey:@"attributes"];
// The href of an attribute object is the URL for the sample audio file
[self setLink:[sampleDict objectForKey:@"href"]];
}
}
You can now build and run the application. Once it starts running, flip to Apple's RSS
feed. Voilà! Now that the RSSItem is grabbing the link, you can tap an entry in the table
view and listen to a sample clip. (The debugger will spit out a lot of angry nonsense about
not finding functions or frameworks, but after a moment, it will shut up, and the clip will
play.)
Notice that, in MVCS (and in MVC, too), model objects are responsible for constructing
themselves given a stream of data. For example, RSSChannel conforms to both
NSXMLParserDelegate and JSONSerializable - it knows how to pack and un-
pack itself from XML and JSON data. Also, think about BNRItem instances in Homep-
wner : they implemented the methods from NSCoding , so that they could be written to
disk.
A model object, then, provides the instructions on how it is to be transferred into and out
of different formats. Model objects do this so that when a store object transfers them to or
from an external source, the store doesn't need to know the details of every kind of model
object in an application. Instead, each model object can conform to a protocol that says,
“Here are the formats I know how to be in,” and the store triggers the transfer into or out
of those data formats.
Search WWH ::




Custom Search