XML解析iOS -NSUnknownKeyException

时间:2011-12-04 07:04:45

标签: iphone ios xml xcode ipad

编辑:我发现网址是由字符串发送的,因此我将其更改为NSURL,现在它会解析部分数据,然后点击“NSUnknownKeyException”和{{1 }}。其余的代码是一样的......谢谢!!

嘿所以我想尝试在iOS中运行一个简单的XML解析器。每次我运行它时,我在main中得到SIGABRT,错误为:SIGABRT

我使用了一个简单的教程,这是我的NSInvalidArgument

XMLParser

这是我的video.h文件,以了解我的数据类型..

#import "XMLParser.h"
#import "Video.h"

@implementation XMLParser
@synthesize video, videos;

- (XMLParser *) initXMLParser {
    //[super init];
    // init array of user objects 
    videos = [[NSMutableArray alloc] init];
    return self;
}

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

    if ([elementName isEqualToString:@"video"]) {
        NSLog(@"video element found – create a new instance of User class...");
        video = [[Video alloc] init];
        //We do not have any attributes in the user elements, but if
        // you do, you can extract them here: 
        // user.att = [[attributeDict objectForKey:@"<att name>"] ...];
    }
}



// XMLParser.m
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (!currentElementValue) {
        // init the ad hoc string with the value     
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    } else {
        // append value to the ad hoc string    
        [currentElementValue appendString:string];
    }
    NSLog(@"Processing value for : %@", string);
}  



//XMLParser.m
- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"videos"]) {
        // We reached the end of the XML document
        return;
    }

    if ([elementName isEqualToString:@"video"]) {
        // We are done with user entry – add the parsed user 
        // object to our user array
        [videos addObject:video];
        // release user object
       // [video release];
        video = nil;
    } else {
        // The parser hit one of the element values. 
        // This syntax is possible because User object 
        // property names match the XML user element names   
        [video setValue:currentElementValue forKey:elementName];
    }

   // [currentElementValue release];
    currentElementValue = nil;
}

@end

// end of XMLParser.m file

@end

另请注意,这是在iOs 5中 这是我的XML:

#import <UIKit/UIKit.h>


@interface Video : NSObject {

    NSInteger videoID;
    NSString *videotitle;   //Same name as the Entity Name.
    NSString *description;  //Same name as the Entity Name.
    NSString *userid;
    NSString *uploadtime;
    NSString *channel;
    NSInteger *viewed;
    NSInteger *liked;
    NSString *videosource;
    NSString *smallthumbnail;   //Same name as the Entity Name.
    NSString *largethumbnail;


}

@property (nonatomic, readwrite) NSInteger videoID;
@property (nonatomic, retain) NSString *videotitle;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *userid;
@property (nonatomic, retain) NSString *uploadtime;
@property (nonatomic, retain) NSString *channel;
@property (nonatomic, readwrite) NSInteger *viewed;
@property (nonatomic, readwrite) NSInteger *liked;
@property (nonatomic, retain) NSString *videosource;
@property (nonatomic, retain) NSString *smallthumbnail;
@property (nonatomic, retain) NSString *largethumbnail;

1 个答案:

答案 0 :(得分:0)

在您的视频课程中,请执行以下功能:

- (void) setValue:(id)value forUndefinedKey:(NSString *)key {
    NSLog(@"Missing key %@", key);
}

然后,您可以查看您的班级没有响应的键。