NSUnknownKeyException - setValue: forUndefinedKey在类文件



我是iPhone开发的新手,我得到这个错误输出。我知道发生了什么事,只是不知道如何解决。

 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 
'[<loginData 0x6b1c> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key UserEMailAddress.'

基本上我正在使用XML解析器,并试图将数据存储到我创建的类'loginData '。我遇到的第一个元素是UserEMailAddress,代码试图将该值存储到同名的类变量UserEMailAddress中。但是它会抛出这个错误。

很明显,当我创建我的类时出了问题。不知何故,我猜事情没有设置正确,它不能输入数据到类。为了创建loginData,我做了一个file->new-> class对象。

下面是类代码。

loginData.h

#import <Foundation/Foundation.h>
@interface loginData : NSObject{
  NSString *UserEMailAddress;
  NSString *SessionUID;
  NSString *SessionExpirationUTCDT;
 }
@property (nonatomic, retain) NSString *UserEMailAddress;
@property (nonatomic, retain) NSString *SessionUID;
@property (nonatomic, retain) NSString *SessionExpirationUTCDT;
@end

loginData.m

  #import "loginData.h"
  @implementation loginData
  @synthesize UserEMailAddress=_UserEMailAddress;
  @synthesize SessionUID=_SessionUID;
  @synthesize SessionExpirationUTCDT=_SessionExpirationUTCDT;
  @end

非常简单的东西,没有太复杂的。

崩溃前访问的最后一个方法在XMLParser中,它是.

- (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:@"LoginResponse"]) {
       // We reached the end of the XML document
       return;
   }
  //if ([elementName isEqualToString:@"user"]) {
      // We are done with user entry – add the parsed user 
      // object to our user array
      //[users addObject:user];
      // release user object
      //[user release];
      //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   
      [loginData setValue:currentElementValue forKey:elementName];
  }   
  currentElementValue = nil;
}

让我们看看你的代码:

loginData.h

@interface loginData : NSObject{
  NSString *UserEMailAddress;
  ...
 }
@property (nonatomic, retain) NSString *UserEMailAddress;
...
@end

loginData.m

@implementation loginData
@synthesize UserEMailAddress=_UserEMailAddress;
...
@end

如果您注意到,您将该属性合成为一个缺失的变量名称_UserEMailAddress。您应该将其替换为UserEMailAddress,然后您的代码将正常工作。

我正面临着同样的问题,因为我从头文件- *.h -文件手动重命名了一个属性。通过这样做,似乎在某些地方手动重命名的属性仍然使用原始名称引用。

为了解决这个问题,我将属性重命名为原始名称,并从源文件开始重命名- *.m。

这里的问题可能是您没有正确地遵守键值编码原则。例如,本文档描述一个“key”必须以小写字母开头。如果您不按照文档的要求去做,尤其是像KVC&mdash这样基本的东西,您很可能会遇到奇怪的,有时是无法解释的行为。

我设法解决它,因为我使用相同的例子。我所做的是使用xml中的名称作为XMLParser.h中的变量例如,如果您的XML文件是这样的:

<Foo>
 <Param1>
  <UserEMailAddress>foo</UserEMailAddress>
  <SessionUID>foo</SessionUID>
  <SessionExpirationUTCDT>foo</SessionExpirationUTCDT>
 </Param1>
</Foo>

您的loginData.h将保留:

#import <Foundation/Foundation.h>
@interface loginData : NSObject{
  NSString *UserEMailAddress;
  NSString *SessionUID;
  NSString *SessionExpirationUTCDT;
 }
@property (nonatomic, retain) NSString *UserEMailAddress;
@property (nonatomic, retain) NSString *SessionUID;
@property (nonatomic, retain) NSString *SessionExpirationUTCDT;
@end

对于我来说,在我的项目中,我收到了空的和正确的数据,例如:

2013-03-29 20:23:40.862 ******* [5271:907] Preccessing value for : 
2013-03-29 20:23:40.863 *******[5271:907] Preccessing value for : 20/03/13
2013-03-29 20:23:40.865 *******[5271:907] Preccessing value for : 
2013-03-29 20:23:40.866 *******[5271:907] Preccessing value for : 14:07
2013-03-29 20:23:40.868 *******[5271:907] Preccessing value for :  

这是解析过的数据,而不是标签,但这是一个开始

进入loginData视图控制器xib的连接检查器&删除UserEMailAddress的引用。之后它将运行。试一试……:)

最新更新