解析XML会导致错误

时间:2010-11-08 16:12:24

标签: objective-c xml cocoa parsing

抱歉我的英语不好。我来自德国,在解析我的xml文件时遇到了一些问题。我使用最新版本的iOS SDK,包括Xcode。

解析我的xml文件会导致一个众所周知的错误,称为“请求成员'切片',而不是结构或联合”

似乎我已经忘记了什么,初始化我的Delgegate Class? 如果有人可以帮助我,那会很棒。 这是我得到的唯一错误...... narrf ..

以下是与此问题相关的一些代码:

我的XMLParser .h文件

#import <UIKit/UIKit.h>

@class XMLAppDelegate, Slice;

@interface XMLParser : NSObject <NSXMLParserDelegate>{

    NSMutableString *currentElementValue;

    XMLAppDelegate *appDelegate;
    Slice *aSlice; 
}

- (XMLParser *) initXMLParser;

@end

此处.m文件的某些部分显示错误:

#import "XMLParser.h"
#import "XMLAppDelegate.h"
#import "Slice.h"

@implementation XMLParser

- (XMLParser *) initXMLParser { 
    [super init];
    appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
    return self;
}

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

    if([elementName isEqualToString:@"Channel"]) {
        //Initialize the array.
        appDelegate.slices = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Slice"]) {

        //Initialize the slice.
        aSlice = [[Slice alloc] init];

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

        NSLog(@"Reading id value :%i", aSlice.sliceID);
    }

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

我尝试使用委托的切片数组后出现错误。

如果有人知道该做什么会很棒, 这件小事让我感到恶心。 ^^

1 个答案:

答案 0 :(得分:0)

解析XML文件没有做任何事情,因为你没有那么远。您的程序只会在运行时解析XML;你的代码还没有运行,因为它没有编译。

错误在于编译(这就是你在Build Results中看到它的原因),并且是一个语法错误:你试图使用编译器无法识别的名称对你使用它的地方有效

具体做法是:

        appDelegate.slices = [[NSMutableArray alloc] init];

编译器不知道appDelegate具有slices属性;因此,它依赖于它的C根并且抱怨你正在尝试对不是结构的东西进行结构访问。您在代码中显示导入了AppDelegate类的标头,因此问题必须是您没有在该类的slices中声明@interface属性。

相关问题