我很难将XML文档解析到我的项目中。我得到了以下错误:
终止应用程序,原因是:“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%地解决了这个问题。
任何帮助都将不胜感激。
干杯,山姆
发布于 2013-01-29 02:23:38
不经意间,看起来你的用户对象实际上没有一个名为“morph”的属性。再次检查拼写是否正确,它实际上是运行时的正确类。
发布于 2013-01-29 02:23:55
您的用户类是否为键"morph“实现KVO?
您的用户类需要被认为是KVC。
你能张贴你的用户类的界面和实现吗?那会很方便。
关于实现KVO的更多信息
发布于 2013-01-29 12:06:05
我发现这个问题是由于这一行代码造成的:
[user setValue:currentElementValue forKey:elementName];我已经暂时将它从我的代码中删除,直到我理解它为什么会使程序抛出异常为止。
https://stackoverflow.com/questions/14574569
复制相似问题