Jimmy
Jimmy

Reputation: 893

I Can't Insert an Object in a NSMutableArray when I parse a XML

The Code:

    ...
    else if ([elementName isEqualToString:@"photo"]) {
        NSLog(@"Trying to add photo");
        NSLog(@"Prev: %d",[currentItem.photoList count]);
        NSLog(@"Author: %@",[currentPhotoItem author ]);
        [currentItem.photoList addObject:currentPhotoItem];
        NSLog(@"Next: %d",[currentItem.photoList count]);
    }

The Log:

2011-10-11 09:49:13.553 ECG[4862:b303] Trying to add photo

2011-10-11 09:49:13.553 ECG[4862:b303] Prev: 0

2011-10-11 09:49:13.554 ECG[4862:b303] Author: Fernando Blanco

2011-10-11 09:49:13.573 ECG[4862:b303] Next: 0

The method addObject doesn't add objects to the NSMutableArray...

"photoList" is a NSMutableString defined in the class:

 @interface GalleryRSSItem : NSObject {

      NSString *title;
      NSString *imgtn;
      NSString *category;
      NSString *url;
      NSString *date;

      NSMutableArray *photoList;
 }
 @property (nonatomic, retain) NSString *title;
 @property (nonatomic, retain) NSString *imgtn;
 @property (nonatomic, retain) NSString *category;
 @property (nonatomic, retain) NSString *url;
 @property (nonatomic, retain) NSString *date;
 @property (nonatomic, retain) NSMutableArray *photoList;
 @end

And the objects are initialized in the parser didStartElemnt method

- (void) parser:(NSXMLParser *) Parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"gallery"]) {
         currentItem = [[GalleryRSSItem alloc] init];
         currentNodeContent = [[NSMutableArray alloc] init];
    }
    if ([elementName isEqualToString:@"photo"]) {
        currentPhotoItem = [[PhotoRSSItem alloc] init];
        currentNodeContent = [[NSMutableArray alloc] init];
    }
}

Upvotes: 0

Views: 84

Answers (2)

Gobra
Gobra

Reputation: 4261

I guess, either currentItem or currentItem.photoList is NIL. Obj-C allows to call methods on nil objects and the count call will return 0 in such case.

Upvotes: 1

Bartek
Bartek

Reputation: 1996

Check if you have properly initialized currentItem.photoList array.

Upvotes: 1

Related Questions