2011-01-13 62 views
2

我有一個XML文件,我解析。NSXMLParser問題

NSURL *url = [NSURL URLWithString:@"url.xml"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; 
self.downloadData = [[NSMutableData alloc] initWithLength:0]; 
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

我有所有的連接工作,我以爲我有解析工作,但我有問題,不知道我做錯了。

我didStartElement:

-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict { 
    if ([elementName isEqualToString:kimgurl]) 
    { 
     elementFound = YES; 
    } 
} 

foundCharacter:

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string { 
if (elementFound == YES) { 
     if(!currentValue) 
     { 
      currentValue = [[NSMutableString alloc] init]; 
     } 

     [currentValue appendString: string]; 
    } 
} 

然後didEndElement:

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 
    if (elementFound) { 
     if ([elementName isEqualToString:kimgurl]) { 
      NSLog(@"imgurl: %@", currentValue); 
      imageItems.imageURL = currentValue; 
      NSLog(@"elname: %@", elementName); 
      NSLog(@"img: %@", kimgurl); 
      [currentValue setString:@""]; 
      NSLog(@"imageitem: %@", imageItems.imageURL); 
     } 
    } 
} 

我有NSLogs那裏,因爲imageItems.imageURL爲空。這是一個類似於這樣的類文件。

ImageItems.h

@interface ImageItems : NSObject { 
    //parsed data 
    NSString *imageURL; 
} 

@property (nonatomic, retain) NSString *imageURL; 

@end 

ImageItems.m

#import "ImageItems.h" 

@implementation ImageItems  
@synthesize imageURL; 

-(void)dealloc 
{ 
    [imageURL release]; 
    [super dealloc]; 
} 

@end 

你可以通過我的代碼告訴我是新來的Objective-C。

currentValue具有我正在尋找的值。爲什麼imageItems爲空?我錯過了什麼?

回答

0

我在代碼中的任何地方都看不到「imageItems」的賦值。您可能需要在某個時刻將其分配給ImageItems的實例。也許用self.imageItems = [[[ImageItems alloc] init] autorelease]imageItems = [[ImageItems alloc] init];

+0

我這樣做,我得到EXC_BAD_ACCESS。可能與將ImageItems作爲@class調用有關嗎? – user574947 2011-01-14 03:50:12