iOS NSXML dictionary not adding to array

152 Views Asked by At

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
1

There are 1 best solutions below

1
Deepesh Gairola On

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

currentPressRelease = [FCPressRelease alloc] init];