Thursday, 15 January 2015

xml parsing - string converted to NSURL and stored as property successful, later returns nil -



xml parsing - string converted to NSURL and stored as property successful, later returns nil -

i parsing strings feed, converting them url, , storing them property of feeditem. initially, converted url , stored, later, when access property, nil.

feeditem.h

@interface feeditem : nsobject @property (nonatomic, strong) nsstring* author; @property (nonatomic, strong) nsurl* imageurl; @property (nonatomic, strong) nsstring* datepublished; @end

parser.m

- (void)parser:(nsxmlparser *)parser didstartelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qualifiedname attributes:(nsdictionary *)attributedict { // custom blog object initialized here if ([elementname isequaltostring:@"entry"]) { self.blogentry = [[feeditem alloc] init]; } // parse image url accompanies blog entries self.blogentry.imageurl = [nsurl urlwithstring:[attributedict objectforkey:@"url"]]; if ([[nsurl urlwithstring:[attributedict objectforkey:@"url"]] iskindofclass:[nsurl class]]) { nslog( @"converted url" ); if ([self.blogentry.imageurl iskindofclass:[nsurl class]]) { nslog(@"property of object url"); }else if (!self.blogentry.imageurl) { nslog(@"url becomes nil"); }else{ nslog(@"property of object not url"); } } }

this prints "converted url" , "property of object url" every time should. however, later in same file:

- (void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname { if([elementname isequaltostring:@"entry"]) { // individual blog has been parsed , pointer added parsedresults array if ([self.blogentry.imageurl iskindofclass:[nsurl class]]) { nslog( @"url passed" ); }else if (!self.blogentry.imageurl) { nslog( @"is nil" ); }else{ nslog(@"no luck"); } [self.parsedresults addobject:self.blogentry]; } }

this prints "is nil" every time.

here illustration of 1 of urls beingness parsed: url='http://2.bp.blogspot.com/-hlneykf6jyk/urkj0nza_ki/aaaaaaaaady/aakm6mnitlo/s72-c/ananya's+illustration.jpg'

i know there can issues if url has special character, because successful @ first, figured shouldn't issue.

i'm new objective-c...what missing??

my guess self.blogentry getting deallocated.

this line:

self.blogentry = [[feeditem alloc] init];

is replacing @ self.blogentry. possible parser calls more 1 time before calling "didendelement" method?

or, blogentry specified strong property on self? if not, deallocated @ end of method after it's created.

xml-parsing

No comments:

Post a Comment