我无法将XML文档解析为我的项目。我收到以下错误:
由于未捕获的异常'NSUnknownKeyException'而终止应用, 原因:'[setValue:forUndefinedKey:]:这个类是 键值变换不符合键值编码。'
以下是我的XML文档的片段:
<?xml version="1.0" encoding="utf-8"?>
<CAS version="2.1" avatar="anna">
<frames count="111" signCount="3">
<signStart index="0" gloss="mug" />
<frame index="0" isComplete="true" time="0" duration="20" boneCount="74"
morphCount="51">
<morph name="aaa" amount="0" />
<morph name="ooo" amount="0" />
<morph name="pout" amount="0" />
<morph name="eee" amount="0" />
<morph name="cgng" amount="0" />
这是我的XMLParser.h文件中的代码:
#import <Foundation/Foundation.h>
@class User;
@interface XMLParser : NSObject {
// an ad hoc string to hold element value
NSMutableString *currentElementValue;
// user object
User *user;
// array of user objects
NSMutableArray *times;
NSMutableArray *durations;
NSMutableArray *wheres;
}
@property (nonatomic, retain) User *user;
@property (nonatomic, retain) NSMutableArray *times;
@property (nonatomic, retain) NSMutableArray *durations;
@property (nonatomic, retain) NSMutableArray *wheres;
- (XMLParser *) initXMLParser;
@end
和XMLParser.m
#import "XMLParser.h"
#import "User.h"
@implementation XMLParser
@synthesize user, times, durations, wheres;
- (XMLParser *) initXMLParser {
self = [super init];
if(self){
// init array of user objects
times = [[NSMutableArray alloc] init];
durations = [[NSMutableArray alloc] init];
// wheres = [[NSMutableArray alloc] init];
}
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if(![elementName isEqual:@"frame"])
return;
NSLog(@"user element found – create a new instance of User class...");
user = [[User alloc] init];
//We do not have any attributes in the user elements, but if
// you do, you can extract them here:
NSString *time = [attributeDict objectForKey:@"time"];
NSString *duration = [attributeDict objectForKey:@"duration"];
//NSString *where = [attributeDict objectForKey:@"where"];
[times addObject:time];
[durations addObject:duration];
// [wheres addObject:where];
}
- (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);
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"CAS"]) {
// We reached the end of the XML document
NSLog(@"names array: %@", times);
NSLog(@"prices array: %@", durations);
// NSLog(@"wheres array: %@", wheres);
return;
}
if ([elementName isEqualToString:@"frame"]) {
// We are done with user entry – add the parsed user
// object to our user array
//[users addObject:user];
//user = 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
[user setValue:currentElementValue forKey:elementName];
}
currentElementValue = nil;
}
我认为它与我的xml文件有关,但对于确切的问题我不是100%。
非常感谢任何帮助。
干杯, 萨姆
答案 0 :(得分:0)
看起来你的User对象实际上没有名为'morph'的属性。仔细检查你拼写错误,它实际上是运行时正确的类。
答案 1 :(得分:0)
答案 2 :(得分:0)
我发现问题是由于这行代码所致:
[user setValue:currentElementValue forKey:elementName];
我暂时将其从代码中删除,直到我理解为什么它会让程序抛出异常