-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSSItem.m
More file actions
97 lines (79 loc) · 3.07 KB
/
RSSItem.m
File metadata and controls
97 lines (79 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// RSSItem.m
// Nerdfeed
//
// Created by THOMAS PENG on 6/18/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "RSSItem.h"
@implementation RSSItem
@synthesize title, link, parentParserDelegate, publicationDate;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"\t\t%@ found a %@ element", self, elementName);
if ([elementName isEqual:@"title"]) {
currentString = [[NSMutableString alloc] init];
[self setTitle:currentString];
} else if ([elementName isEqual:@"link"]) {
currentString = [[NSMutableString alloc] init];
[self setLink:currentString];
} else if ([elementName isEqual:@"pubDate"]) {
// Create the string, but do not put it into an invar yet
currentString = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[currentString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
// If the pubDate ends, use a date formatter to turn it into an NSDate
if ([elementName isEqualToString:@"pubDate"]) {
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
}
[self setPublicationDate:[dateFormatter dateFromString:currentString]];
}
currentString = nil;
if ([elementName isEqual:@"item"] || [elementName isEqual:@"entry"])
[parser setDelegate:parentParserDelegate];
}
- (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"]];
}
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:title forKey:@"title"];
[aCoder encodeObject:link forKey:@"link"];
[aCoder encodeObject:publicationDate forKey:@"publicationDate"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
[self setTitle:[aDecoder decodeObjectForKey:@"title"]];
[self setLink:[aDecoder decodeObjectForKey:@"link"]];
[self setPublicationDate:[aDecoder decodeObjectForKey:@"publicationDate"]];
}
return self;
}
- (BOOL)isEqual:(id)object
{
// Make sure we are comparing an RSSItem!
if (![object isKindOfClass:[RSSItem class]])
return NO;
// Now only return YES if the links are equal.
return [[self link] isEqual:[object link]];
}
@end