我有一个正在解析的xml文件。
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;
@endImageItems.m
#import "ImageItems.h"
@implementation ImageItems
@synthesize imageURL;
-(void)dealloc
{
[imageURL release];
[super dealloc];
}
@end从我的代码可以看出,我对目标-c是新的。
currentValue有我想要的价值。为什么imageItems为空?我遗漏了什么?
发布于 2011-01-14 00:59:38
在您的代码中,我看不到"imageItems“的赋值。您可能需要在某个时候将其分配给ImageItems的一个实例。也许是用self.imageItems = [[[ImageItems alloc] init] autorelease]或imageItems = [[ImageItems alloc] init];
https://stackoverflow.com/questions/4686060
复制相似问题