0

我已经尝试了很长时间了,但是我无法成功地发布到我的服务器。我已经阅读Restkit的github 对象映射概述Gist restKit 教程

我的问题

我正在尝试发布登录信息(昵称、硬件 ID、电话型号)并从我的服务器获取 AccountId。但是我从控制台得到了这个响应:

GuessTheImage[6832:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AccountsClass 0x8c5cba0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key AccountInfo.'

我认为是什么问题

我相信我在映射部分之前搞砸了存储数据的方式。但我不确定具体在哪里。

我正在尝试发布的 JSON 应该是这样的

{
  "DeviceType": "sample string 1",
  "HardwareId": "sample string 2",
  "NickName": "sample string 3",
  "AccountId": 4
}

程序概述 我在 AccountClass 中创建了用于映射的变量。在用户单击登录按钮后,我从 loginviewcontroller 获取设备类型、hardwareId 和昵称,并将这些变量分配给 AccountClass 中的变量,然后将其映射为 POST。AccountClass.h

#import <Foundation/Foundation.h>

@interface AccountsClass : NSObject

@property (nonatomic, strong,retain)NSString *DeviceType;
@property (nonatomic,strong)NSNumber *AccountId;
@property (nonatomic,strong) NSString *NickName;
@property (nonatomic,strong) NSNumber *HardwareId;

@end

登录视图控制器.h

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UIButton *submitButton;
@property (nonatomic,readonly) NSUUID *identifierForVendor;
@property(nonatomic, readonly, retain) NSString *model;
@property (nonatomic,readonly,retain)NSString *StoreIdentifierForVendor;
@property (nonatomic,readonly,retain)NSString *StoreTheModel;
- (IBAction)submit:(id)sender;
@property (nonatomic,strong)NSString *nickname;
@end

登录视图控制器.m

#import "LoginViewController.h"
#import <RestKit/RestKit.h>
#import "AccountsClass.h"
@interface LoginViewController ()

@end

@implementation LoginViewController
@synthesize usernameTextField;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissmissKeyboard)];
    [self.view addGestureRecognizer:tap];
    [usernameTextField setDelegate:self];


}
-(void)loadPostRequest
{
    _StoreIdentifierForVendor = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
    _StoreTheModel = [UIDevice currentDevice].model;
    _nickname = usernameTextField.text;
     AccountsClass *AccountInfo = [[AccountsClass alloc] init];
    AccountInfo.NickName = _nickname;
    NSNumberFormatter *hardwareIdentification = [[NSNumberFormatter alloc]init];
    [hardwareIdentification setNumberStyle:NSNumberFormatterScientificStyle];
    NSNumber *hwreID =[hardwareIdentification numberFromString:_StoreIdentifierForVendor];
    AccountInfo.HardwareId = hwreID;
    AccountInfo.DeviceType =_StoreTheModel;
      RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[AccountsClass class]];
   [responseMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]];

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *AccountDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:statusCodes];


    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
    [requestMapping addAttributeMappingsFromArray:@[@"AccountInfo.NickName", @"AccountInfo.HardwareId", @"AccountInfo.DeviceType",@"AccountInfo.AccountId"]];
    // For any object of class Article, serialize into an NSMutableDictionary using the given mapping and nest
    // under the 'article' key path
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[AccountInfo class] rootKeyPath:nil method:RKRequestMethodAny];
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://picquiz.azurewebsites.net"]];
                                [manager addRequestDescriptor:requestDescriptor];
                                [manager addResponseDescriptor:AccountDescriptor];
                                // POST to create
                                [manager postObject:AccountInfo path:@"/Accounts" parameters:nil success:nil failure:nil];

}
-(void)dissmissKeyboard
{
    [usernameTextField resignFirstResponder];
}

- (IBAction)submit:(id)sender {
    [self loadPostRequest];
}
@end

我会感谢你的帮助。

4

1 回答 1

1

您的问题似乎是:

[requestMapping addAttributeMappingsFromArray:@[@"AccountInfo.NickName", @"AccountInfo.HardwareId", @"AccountInfo.DeviceType",@"AccountInfo.AccountId"]];

因为您尝试发布的数据没有这样的嵌套数据。看起来应该是:

[requestMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]];
于 2013-10-23T13:41:26.503 回答