iphone - NSUnknownKeyException - 设置值 : forUndefinedKey in classfile

标签 iphone objective-c ios xcode4.2 nsxmlparser

<分区>

我是 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 中。但它抛出了那个错误。

显然,当我创建我的类(class)时出了点问题。不知何故,我猜事情没有正确设置,它无法将数据输入到类(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

登录数据.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;
}

最佳答案

让我们看看您的代码:

登录数据.h

@interface loginData : NSObject{
  NSString *UserEMailAddress;
  ...
 }

@property (nonatomic, retain) NSString *UserEMailAddress;
...

@end

登录数据.m

@implementation loginData

@synthesize UserEMailAddress=_UserEMailAddress;
...

@end

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

关于iphone - NSUnknownKeyException - 设置值 : forUndefinedKey in classfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8433427/

相关文章:

iphone - 如何正确格式化这些日期?

objective-c - 鼠标悬停时的 NSPopOver

iOS 将不同设备之间的数据共享到同一个应用程序

iphone - 控制帧速率或限制 AVCaptureVideoPreviewLayer

ios - 在 Mapbox (iOS) 上将方向箭头添加到当前位置

iphone - 在Cocos2d中在屏幕上画一条直线?

iphone - 您可以通过编程方式访问 Cocoa Touch 中的标准本地化字符串吗?

ios - 在 iOS 7 上读取视频元数据 - 始终为空

ios - 枚举按钮的 NSMutableArray 添加一些到另一个 NSMutableArray

ios - AVPlayer(和 MPMoviePlayerController)试图缓冲整个(流媒体)视频,内存不足