【问题标题】:NSXMLParser troubleNSXMLParser 问题
【发布时间】:2012-01-05 04:46:00
【问题描述】:

我使用了一个示例来了解NSXMLParser,但我不需要将它用于appDelegate,而是用于其他 ViewController,这似乎不起作用:

@implementation XMLParser

- (XMLParser *) initXMLParser {

    [super init];

    viewController = (ViewController *)[[UIApplication sharedApplication] delegate];

    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"Books"]) {
        //Initialize the array.
        viewController.books = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Book"]) {

        //Initialize the book.
        aBook = [[Book alloc] init];

        //Extract the attribute here.
        aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];

        NSLog(@"Reading id value :%i", aBook.bookID);
    }

    NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

    NSLog(@"Processing Value: %@", currentElementValue);

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"Books"])
        return;

    //There is nothing to do if we encounter the Books element here.
    //If we encounter the Book element howevere, we want to add the book object to the array
    // and release the object.
    if([elementName isEqualToString:@"Book"]) {
        [viewController.books addObject:aBook];

        [aBook release];
        aBook = nil;
    }
    else 
        [aBook setValue:currentElementValue forKey:elementName];

    [currentElementValue release];
    currentElementValue = nil;
}

viewController.books 解析后没有任何对象。为什么?

【问题讨论】:

  • 您正在将 AppDelegate 类型转换为您称为 ViewController 的东西 - 看起来很可疑。

标签: ios nsxmlparser


【解决方案1】:

我可能会摆脱 initXMLParser 代码。相反,我会将视图控制器设为实例变量,并在初始化解析器时设置它。

MyParser *parser = [[MyParser alloc] init]; 
[parser setViewController:self]; 

另一种方法是让您的 ViewController 成为 MyParser 的委托。在 MyParser.h 中你可以声明一些委托方法:

@protocol MyParserDelegate
- (void)foundBooks:(NSMutableArray *)booksArray;
- (void)foundBook:(Book *)aBook; 
@end

@interface MyParser {
     id<MyParserDelegate> delegate; 
     NSMutableArray *bookArray; 
     Book *aBook; 
     ... 
}
@end

然后,您的视图控制器将是一个委托:

@interface ViewController : UIViewController <MyParserDelegate> { 

...
}
@end 

在 ViewController.m 中,您将实现 foundBooks: 和 foundBook:。

然后,在您的解析器中,您将调用委托方法:

if([elementName isEqualToString:@"Books"]) {
    [self.delegate foundBooks:bookArray]; 
    ...
} 

if([elementName isEqualToString:@"Book"]) {
    [self.delegate foundBook:aBook]; 
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多