【问题标题】:NSXMLParser terminating due to NSUnknownKeyExceptionNSXMLParser 由于 NSUnknownKeyException 而终止
【发布时间】:2013-01-29 02:11:49
【问题描述】:

我在将 XML 文档解析到我的项目时遇到问题。我收到以下错误:

由于未捕获的异常“NSUnknownKeyException”而终止应用程序, 原因:'[setValue:forUndefinedKey:]:这个类是 不符合键变形的键值编码。'

这是我的 XML 文档的 sn-p:

<?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% 的确切问题。

任何帮助将不胜感激。

干杯, 山姆

【问题讨论】:

    标签: ios objective-c xml xcode nsxmlparser


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      What your User class needs to have to be considered KVC

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

      More about implementing KVO

      【讨论】:

        【解决方案3】:

        我发现问题出在这行代码上:

        [user setValue:currentElementValue forKey:elementName];
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-17
          • 2012-01-04
          • 2016-08-20
          • 1970-01-01
          • 2012-06-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多