Iphone 使用RestKit时出错';RKClient post参数的RKObjectMapping和RKObjectSerializer

Iphone 使用RestKit时出错';RKClient post参数的RKObjectMapping和RKObjectSerializer,iphone,objective-c,json,restkit,Iphone,Objective C,Json,Restkit,我有这样一个请求对象: @interface MyUpdate @property (nonatomic, copy) NSString* name; @property (nonatomic, assign) int value; @end @interface MyRequest @property (nonatomic, assign) int index; @property (nonatomic, retain) MyUpdate* update; @end 我正在使用RKObje

我有这样一个请求对象:

@interface MyUpdate
@property (nonatomic, copy) NSString* name;
@property (nonatomic, assign) int value;
@end

@interface MyRequest
@property (nonatomic, assign) int index;
@property (nonatomic, retain) MyUpdate* update;
@end
我正在使用RKObjectMapping和RKObjectSerializer创建JSON字符串,并在帖子中使用它:

RKObjectMapping* updateMapping = [RKObjectMapping mappingForClass:[MyUpdate class]];
[updateMapping mapForKeyPath:@"name" toAttribute:@"Name"];
[updateMapping mapForKeyPath:@"value" toAttribute:@"Value"];

RKObjectMapping* requestMapping = [RKObjectMapping mappingForClass:[MyRequest class]];
[requestMapping mapForKeyPath:@"index" toAttribute:@"Index"];
[requestMapping mapKeyPath:@"update" toRelationship:@"Update" withMapping:updateMapping];

RKObjectSerializer* serializer = [RKObjectSerializer serializerWithObject:request mapping:requestMapping];

[[RKClient sharedClient] post:requestPath params:[serializer serializationForMIMEType:RKMIMETypeJSON error:nil] delegate:self];
request
是my
MyRequest
类的一个实例
requestPath
只是一个
NSString

我一直收到这样一个错误,说一个密钥对MyUpdate无效,即使我已经映射了它。我是否缺少使用RKObjectMapping的一些关键步骤?

我使用“RKObjectLoader”来完成这项工作。下面是一个例子:

    MyUpdate *myUpdate = [[MyUpdate alloc] init];

    [[RKClient sharedClient].HTTPHeaders setValue:RKMIMETypeJSON forKey:@"Content-Type"];      

    // Prepare the request
    NSMutableDictionary *requestDictionary = [[NSMutableDictionary alloc] init];
    [requestDictionary setObject:yourIndexVar forKey:@"Index"];
    [requestDictionary setObject:fileName forKey:@"fileName"];

    NSError* error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:requestDictionary options:NSJSONWritingPrettyPrinted error:&error];

    NSString *JSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    RKParams *params = [RKRequestSerialization serializationWithData:[JSON dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON]; 


    // Prepare the response mapping
    RKObjectMapping* objectMapping = [RKObjectMapping mappingForClass:MyUpdate class]];
    [objectMapping mapKeyPath:@"name" toAttribute:@"Name"];    
    [objectMapping mapForKeyPath:@"value" toAttribute:@"Value"];

    [objectMapping mapKeyPath:@"update" toRelationship:@"Update" withMapping:objectMapping];


    RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"BASE_URL"];  
    [manager setClient:[RKClient sharedClient]];
    [manager.mappingProvider setMapping:objectMapping forKeyPath:@"****Result"];  

    RKObjectLoader *objectLoader = [manager loaderWithResourcePath:@"RELATIVE_PATH"];   

    // For example:
    // BASE_URL = "http://mysite.com/"
    // RELATIVE_PATH (service end-point uri) = "/ServiceName/SaveFile/"
    // **** = "SaveFile"

    objectLoader.targetObject = myUpdate;
    objectLoader.method = RKRequestMethodPOST;
    objectLoader.params = params;
    objectLoader.delegate = self;

    @try 
    {
        [objectLoader send];
    }
    @catch (NSException *exception) 
     {
         NSLog(@"NSException - Name: %@, Reason: %@", exception.name, exception.reason);
     }