0

我在将 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% 的确切问题。

任何帮助将非常感激。

干杯,山姆

4

3 回答 3

0

我发现问题是由于这行代码造成的:

[user setValue:currentElementValue forKey:elementName];

我暂时将它从我的代码中删除,直到我明白它为什么会让程序抛出异常

于 2013-01-29T12:06:05.703 回答
0

副手看起来你的用户对象实际上并没有一个名为“变形”的属性。仔细检查你的拼写是否正确,它实际上是运行时正确的类。

于 2013-01-29T02:23:38.957 回答
0

您的用户类是否为键“变形”实现 KVO?

您的 User 类需要被视为 KVC

你能发布你的用户类的接口和实现吗?那会很方便。

更多关于实施 KVO

于 2013-01-29T02:23:55.673 回答