13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

182CHAPTER 6: Networking: Connections, Data, and the Cloud- (void)parser:(NSXMLParser*)parser foundCDATA:(NSData*)CDATABlock{if ( [CDATABlock length] == 0 )return;}NSString *str = [[NSString alloc] initWithData:CDATABlockencoding:NSUTF8StringEncoding];[self parser:parser foundCharacters:str];The handling of elements’ start and end tags is a little more involved, but ultimately stillfairly simple to understand. An important consideration is that any element names thatcontain hyphens will not be usable as-is; you could replace hyphens with underscores,but I have chosen to convert them to CamelCase to match the rest of the methoddefinition. You can see an implementation of this in Listing 6-13.Listing 6-13. Handling Element Start and End Tags- (NSString*)selectorCleanNameForElementName:(NSString*)elementName{NSMutableString *selStr = [NSMutableString new];@autoreleasepool {// ensure the first character of the element name is uppercased[selStr appendString: [[elementName substringToIndex:1] uppercaseString]];// append the rest of the string (provided it's not 1-character long)if ( [elementName length] > 1 )[selStr appendString: [elementName substringFromIndex:1]];}// handle hyphens properlyNSRange range = [selStr rangeOfString:@"-"];for ( ; range.location != NSNotFound; range = [selStr rangeOfString:@"-"] ){NSString *cap = [[selStr substringWithRange:NSMakeRange(range.location+1,1)]uppercaseString];range.length += 1;[selStr replaceCharactersInRange:range withString:cap];}}return ( selStr );- (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementNamenamespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qNameattributes:(NSDictionary*)attributes{@autoreleasepool {NSString *cleanName = [self selectorCleanNameForElementName:elementName];NSString *selStr = [NSString stringWithFormat:@"start%@WithAttributes:",cleanName];www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!