2013-10-23 72 views
0

我一直在嘗試這種方式已經有一段時間了,但我無法成功發佈到我的服務器。我已閱讀this,github object mapping overview for the RestkitGist restKit tutorial無法使用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.' 

我覺得是這個問題

我相信我搞亂了我的映射部分之前正確的數據存儲方式。但我不確定究竟在哪裏。

,我想後應該是這樣的

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

我創建的變量在AccountClass映射程序的概述JSON。從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 

LoginViewController.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 

LoginViewController.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 

我會感謝你的幫助。

+0

可以顯示您希望從服務器接收到的JSON,以響應POST請求。 – Wain

回答

1

你的問題似乎是:

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

因爲你試圖POST數據沒有嵌套這樣的數據。它看起來應該是:

[requestMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]]; 
+0

再次感謝Wain,您的傑出迴應。我之前嘗試使用響應映射嘗試切換請求映射的位置,但之前並未運行。我可能會一起嘗試其他的東西。 – Matt

相關問題