Parsing XML causes Error

2019-08-28 10:46发布

Sorry for my bad English. I'm from Germany and have some problems with parsing my xml file. I use the latest version of the iOS SDK, including Xcode.

Parsing my xml file causes a well known error, called "Requesting for member 'Slices' in something not a structure or union"

Seems that I have forgotten something, initializing my Delgegate Class ? Would be great if someone could help me through. It's the only error I Get... narrf..

Here is some of the code, relevant for this problem:

My XMLParser .h File

#import <UIKit/UIKit.h>

@class XMLAppDelegate, Slice;

@interface XMLParser : NSObject <NSXMLParserDelegate>{

    NSMutableString *currentElementValue;

    XMLAppDelegate *appDelegate;
    Slice *aSlice; 
}

- (XMLParser *) initXMLParser;

@end

And here some parts of the .m File which shows the error:

#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);
}

The error shows up after I try to use the slices array of my delegate.

Would be great if someone knows what to do, this little thing makes me sick. ^^

1条回答
劫难
2楼-- · 2019-08-28 11:14

Parsing the XML file didn't do anything, because you haven't gotten that far. Your program will only parse XML when it runs; your code has not run yet, because it does not compile.

The error is in compilation (which is why you're seeing it in Build Results), and is a syntax error: You have tried to use a name that the compiler does not recognize as valid for the place where you used it.

Specifically:

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

The compiler doesn't know that appDelegate has a slices property; as such, it falls back on its C roots and complains that you're trying to do a structure access to something that isn't a structure. You show in your code that you imported the header for the AppDelegate class, so the problem must be that you didn't declare the slices property in that class's @interface.

查看更多
登录 后发表回答