1

オブジェクト マッピングに使用した後、RestKit を使用して投稿しようとしましたが、「keyPath のオブジェクト マッピングが見つかりませんでした: ''」というエラーが表示され続けます。

次のコードで iOS の JSON のみを使用して、すでに成功しています。

responseData = [NSMutableData data];

NSURL *url = [NSURL URLWithString:@"http://lalubema.cloudapp.net:9000/Data.soap/Pessoa"];

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"Objective-c test from send Request #1" forKey:@"Nome"];
[params setObject:@"/Date(1200355200000)/" forKey:@"DataNascimento"];   
[params setObject:@"M" forKey:@"Sexo"];
[params setObject:@"(31)2321-2383" forKey:@"Telefone"];

NSError *jsonError;
NSData *requestdata = [NSJSONSerialization dataWithJSONObject:params options:0 error:&jsonError];

NSMutableURLRequest *request;
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestdata];

//this kicks off the request asynchronously
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

RestKit を使用してエラーが発生するコードは次のとおりです。

[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
[RKObjectManager sharedManager].acceptMIMEType = RKMIMETypeJSON;

RKObjectMapping* pessoaMapping = [RKObjectMapping mappingForClass:[Pessoa class]];
pessoaMapping.setDefaultValueForMissingAttributes = YES;
[pessoaMapping mapKeyPath:@"DataNascimento" toAttribute:@"dataNascimento"];
[pessoaMapping mapKeyPath:@"Nome" toAttribute:@"nome"];
[pessoaMapping mapKeyPath:@"Sexo" toAttribute:@"sexo"];
[pessoaMapping mapKeyPath:@"Telefone" toAttribute:@"telefone"];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:pessoaMapping]; 

RKObjectMapping* pessoaSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[pessoaSerializationMapping mapAttributes:@"dataNascimento", @"docIdentidade", @"email", @"foto", @"pid", @"nome", @"senha", @"sexo", @"telefone", nil];    
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:pessoaSerializationMapping forClass:[Pessoa class]];

RKObjectRouter *router = [[RKObjectManager sharedManager] router];

[router routeClass:[Pessoa class] toResourcePath:@"/Pessoa/:id"];
[router routeClass:[Pessoa class] toResourcePath:@"/Pessoa" forMethod:RKRequestMethodPOST];

Pessoa *Novo = [[Pessoa alloc] init];

Novo.nome = @"Objective-c test from send postPessoa #1";
Novo.telefone = @"(31)1234-5678";
Novo.sexo = @"M";

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MM-yyyy"];    

RKDotNetDateFormatter *dotNetFormattedDate = [[RKDotNetDateFormatter alloc] init];
NSString *data = [dotNetFormattedDate stringFromDate:[df dateFromString: @"10-10-1979"]];

Novo.dataNascimento = data;

// POST to /Pessoa
[[RKObjectManager sharedManager] postObject:Novo delegate:self];

RKLog エラーは次のとおりです。

2012-06-27 17:58:28.539 MyTestApp[6101:fb03] 私は restkit:RKLog.m:33 RestKit を初期化しました...

2012-06-27 17:58:28.589 MyTestApp[6101:fb03] 私は restkit.network.reachability:RKReachabilityObserver.m:369 到達可能性オブザーバーのネットワーク可用性が決定されました

2012-06-27 17:58:41.260 MyTestApp[6101:fb03] W restkit.object_mapping:RKObjectMapper.m:81 マッピング エラーの追加: keyPath のオブジェクト マッピングが見つかりませんでした: ''

2012-06-27 17:58:41.261 MyTestApp[6101:fb03] E restkit.network:RKObjectLoader.m:222 マッピング中にエラーが発生しました: keyPath のオブジェクト マッピングが見つかりませんでした: ''

2012-06-27 17:58:41.261 MyTestApp[6101:fb03] E restkit.network:RKObjectLoader.m:345 ペイロードからサーバー側エラーをマップしようとしているときにエラーが発生しました: keyPath のオブジェクト マッピングが見つかりませんでした: ''

2012-06-27 17:58:41.262 MyTestApp[6101:fb03] エラー ドメイン: エラー ドメイン=org.restkit.RestKit.ErrorDomain コード=1001 「keyPath のオブジェクト マッピングが見つかりませんでした: ''」 UserInfo=0x808fac0 {= RKObjectMapperKeyPath、NSLocalizedDescription=keyPath のオブジェクト マッピングが見つかりませんでした: ''}

4

1 に答える 1

0

主な問題は、次のコード ブロックで表されていると思います。

RKObjectMapping* pessoaMapping = [RKObjectMapping mappingForClass:[Pessoa class]];
pessoaMapping.setDefaultValueForMissingAttributes = YES;
[pessoaMapping mapKeyPath:@"DataNascimento" toAttribute:@"dataNascimento"];
[pessoaMapping mapKeyPath:@"Nome" toAttribute:@"nome"];
[pessoaMapping mapKeyPath:@"Sexo" toAttribute:@"sexo"];
[pessoaMapping mapKeyPath:@"Telefone" toAttribute:@"telefone"];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:pessoaMapping]; 

RKObjectMapping* pessoaSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[pessoaSerializationMapping mapAttributes:@"dataNascimento", @"docIdentidade", @"email", @"foto", @"pid", @"nome", @"senha", @"sexo", @"telefone", nil];    
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:pessoaSerializationMapping forClass:[Pessoa class]];

次のように置き換えてみてください。

RKObjectMapping* pessoaMapping = [RKObjectMapping mappingForClass:[Pessoa class]];
pessoaMapping.setDefaultValueForMissingAttributes = YES;
[pessoaMapping mapKeyPath:@"DataNascimento" toAttribute:@"dataNascimento"];
[pessoaMapping mapKeyPath:@"Nome" toAttribute:@"nome"];
[pessoaMapping mapKeyPath:@"Sexo" toAttribute:@"sexo"];
[pessoaMapping mapKeyPath:@"Telefone" toAttribute:@"telefone"];
// up until this point, the code is the same, changes below
// -------------------------------------------------------------------------------
// map rest of the attributes, considering key paths match attribute names exactly
[pessoaMapping mapAttributes:@"docIdentidade", @"email", @"foto", @"pid", @"senha", nil];
// set the serialization mapping
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[pessoaMapping inverseMapping] forClass:[Pessoa class]];

また、次の行にも別の問題があります。

[router routeClass:[Pessoa class] toResourcePath:@"/Pessoa/:id"];

mapAttributes:呼び出しに見られるように、 Pessoaクラスの識別子属性はpidという名前のようです。ルーターへのリソースパスを設定するとき、RestKit はKVC を使用して:idの値を何に置き換えるかを知らないと思います。

于 2012-06-29T07:42:09.873 に答える