2011-01-13 34 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