1

I have an instance of the NSXML Parser parsing some xml and storing it in an array of objects. The XML has some image URLS and titles that I am trying to add to a dictionary, and then add the dictionary to an array in the data object. The dictionary is being set properly but it won't add to the array. Below is the xml and code:

XML:

http://www.flashalert.net/rss.html?id=3753

Code:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    NSString *string1 = [string stringByReplacingOccurrencesOfString:@"amp;" withString:@""];
    NSString *string2 = [string1 stringByReplacingOccurrencesOfString:@""" withString:@"\""];
    currentNodeContent = (NSMutableString *) [string2 stringByStrippingHTML];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    if ([elementname isEqualToString:@"item"]) {

        isStatus = YES;
        currentPressRelease = [FCPressRelease alloc];
    }

    if ([elementname isEqualToString:@"media:content"]) {

        isMedia = YES;

        media = [[NSMutableDictionary alloc] init];
        [media setValue:[attributeDict valueForKey:@"url"] forKey:@"url"];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if (isStatus)
    {
        if ([elementname isEqualToString:@"pubDate"])
        {
            currentPressRelease.pubDate = currentNodeContent;
        }
        if ([elementname isEqualToString:@"title"])
        {
            currentPressRelease.title = currentNodeContent;
        }
        if ([elementname isEqualToString:@"description"])
        {
            currentPressRelease.summary = currentNodeContent;
        }
        if ([elementname isEqualToString:@"guid"])
        {
            currentPressRelease.guid = currentNodeContent;
        }
    }
    if (isMedia) {

        if ([elementname isEqualToString:@"media:title"])
        {
            [media setValue:currentNodeContent forKey:@"title"];
        }
    }
    if ([elementname isEqualToString:@"media:content"]) {

        [currentPressRelease.images addObject:media];

        media = nil;
    }
    if ([elementname isEqualToString:@"item"]) {

        if (currentPressRelease != nil) {

            [self.posts addObject:currentPressRelease];

            currentPressRelease = nil;
            currentNodeContent = nil;
        }
        else {

            currentPressRelease = nil;
            currentNodeContent = nil;
        }
    }
}

FCPressRelease.h

@interface FCPressRelease : NSObject

@property (strong, nonatomic) NSString *pubDate;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *summary;
@property (strong, nonatomic) NSString *guid;
@property (strong, nonatomic) NSMutableArray *images;

@end

FCPressRelease.m

@implementation FCPressRelease

@synthesize pubDate = _pubDate;
@synthesize title = _title;
@synthesize summary = _summary;
@synthesize guid = _guid;
@synthesize images = _images;

@end
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73
  • `media` is the dictionary you are talking about? If yes could you please let me know what is the type of object `images` in `currentPressRelease.images`? – Deepesh Gairola May 15 '13 at 03:43
  • images is a NSMutableArray. I am trying the dictionaries to the array so that I have an array of dictionaries (image url/title). – Jon Erickson May 15 '13 at 04:02

1 Answers1

0

You should use an init method to complete the initialization process for FCPressRelease

currentPressRelease = [FCPressRelease alloc] init];

Deepesh Gairola
  • 1,252
  • 12
  • 18