我在将 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% 的确切问题。
任何帮助将非常感激。
干杯,山姆