2013-05-15 94 views
1

我有一個NSXML解析器的實例解析一些xml並將其存儲在一個對象數組中。 XML有一些圖像URLS和標題,我嘗試將其添加到字典中,然後將字典添加到數據對象中的數組中。字典正在正確設置,但不會添加到數組中。下面是XML和代碼:iOS NSXML詞典不添加到陣列

XML:

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

代碼:

- (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 
+0

'media'是字典你在說什麼?如果是的話,請讓我知道'currentPressRelease.images'中的對象'images'的類型是什麼? –

+0

圖像是一個NSMutableArray。我正在嘗試數組的字典,以便我有一個字典數組(圖像URL /標題)。 –

回答

0

您應該使用init方法來完成初始化過程FCPressRelease

currentPressRelease = [FCPressRelease alloc] init];

+0

雖然這並沒有解決問題。 –

+0

你能否提供'FCPressRelease'類的來源? 「圖像」的@屬性屬性是什麼? –

+0

它已更新。 –