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

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

私はあなたの助けに感謝します。

4

1 に答える 1

1

あなたの問題は次のようです:

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

POSTしようとしているデータには、そのようなネストされたデータがないためです。次のようになります。

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