17
17
18
18
@interface XMLReader ()
19
19
20
- - (id )initWithError : (NSError **)error ;
21
- - (NSDictionary *)objectWithData : (NSData *)data options : (XMLReaderOptions)options ;
20
+ @property (nonatomic , strong ) NSMutableArray *dictionaryStack;
21
+ @property (nonatomic , strong ) NSMutableString *textInProgress;
22
+ @property (nonatomic , strong ) NSError *errorPointer;
22
23
23
24
@end
24
25
@@ -61,20 +62,19 @@ - (id)initWithError:(NSError **)error
61
62
self = [super init ];
62
63
if (self)
63
64
{
64
- errorPointer = *error;
65
+ self. errorPointer = *error;
65
66
}
66
67
return self;
67
68
}
68
69
69
70
- (NSDictionary *)objectWithData : (NSData *)data options : (XMLReaderOptions)options
70
71
{
71
72
// Clear out any old data
72
-
73
- dictionaryStack = [[NSMutableArray alloc ] init ];
74
- textInProgress = [[NSMutableString alloc ] init ];
73
+ self.dictionaryStack = [[NSMutableArray alloc ] init ];
74
+ self.textInProgress = [[NSMutableString alloc ] init ];
75
75
76
76
// Initialize the stack with a fresh dictionary
77
- [dictionaryStack addObject: [NSMutableDictionary dictionary ]];
77
+ [self . dictionaryStack addObject: [NSMutableDictionary dictionary ]];
78
78
79
79
// Parse the XML
80
80
NSXMLParser *parser = [[NSXMLParser alloc ] initWithData: data];
@@ -89,7 +89,7 @@ - (NSDictionary *)objectWithData:(NSData *)data options:(XMLReaderOptions)option
89
89
// Return the stack's root dictionary on success
90
90
if (success)
91
91
{
92
- NSDictionary *resultDict = [dictionaryStack objectAtIndex: 0 ];
92
+ NSDictionary *resultDict = [self . dictionaryStack objectAtIndex: 0 ];
93
93
return resultDict;
94
94
}
95
95
@@ -102,7 +102,7 @@ - (NSDictionary *)objectWithData:(NSData *)data options:(XMLReaderOptions)option
102
102
- (void )parser : (NSXMLParser *)parser didStartElement : (NSString *)elementName namespaceURI : (NSString *)namespaceURI qualifiedName : (NSString *)qName attributes : (NSDictionary *)attributeDict
103
103
{
104
104
// Get the dictionary for the current level in the stack
105
- NSMutableDictionary *parentDict = [dictionaryStack lastObject ];
105
+ NSMutableDictionary *parentDict = [self . dictionaryStack lastObject ];
106
106
107
107
// Create the child dictionary for the new element, and initilaize it with the attributes
108
108
NSMutableDictionary *childDict = [NSMutableDictionary dictionary ];
@@ -138,40 +138,39 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam
138
138
}
139
139
140
140
// Update the stack
141
- [dictionaryStack addObject: childDict];
141
+ [self . dictionaryStack addObject: childDict];
142
142
}
143
143
144
144
- (void )parser : (NSXMLParser *)parser didEndElement : (NSString *)elementName namespaceURI : (NSString *)namespaceURI qualifiedName : (NSString *)qName
145
145
{
146
146
// Update the parent dict with text info
147
- NSMutableDictionary *dictInProgress = [dictionaryStack lastObject ];
147
+ NSMutableDictionary *dictInProgress = [self . dictionaryStack lastObject ];
148
148
149
149
// Set the text property
150
- if ([textInProgress length ] > 0 )
150
+ if ([self . textInProgress length ] > 0 )
151
151
{
152
152
// trim after concatenating
153
- NSString *trimmedString = [textInProgress stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
153
+ NSString *trimmedString = [self . textInProgress stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
154
154
[dictInProgress setObject: [trimmedString mutableCopy ] forKey: kXMLReaderTextNodeKey ];
155
155
156
156
// Reset the text
157
-
158
- textInProgress = [[NSMutableString alloc ] init ];
157
+ self.textInProgress = [[NSMutableString alloc ] init ];
159
158
}
160
159
161
160
// Pop the current dict
162
- [dictionaryStack removeLastObject ];
161
+ [self . dictionaryStack removeLastObject ];
163
162
}
164
163
165
164
- (void )parser : (NSXMLParser *)parser foundCharacters : (NSString *)string
166
165
{
167
166
// Build the text value
168
- [ textInProgress appendString: string];
167
+ [ self . textInProgress appendString: string];
169
168
}
170
169
171
170
- (void )parser : (NSXMLParser *)parser parseErrorOccurred : (NSError *)parseError
172
171
{
173
172
// Set the error pointer to the parser's error object
174
- errorPointer = parseError;
173
+ self. errorPointer = parseError;
175
174
}
176
175
177
176
@end
0 commit comments