如何使用restkit忽略发布对象上的空属性

我正在恢复原来使用RestKit 0.10的旧项目,现在正在使用RestKit 0.24。 旧的版本仍然有效,但不幸的是RestKit 0.10不是64位兼容的,因此不会提交到AppStore(无论如何现在肯定是时候更新了)。

我无法获得正确发布的对象。 在RestKit 0.10中,没有值的属性没有被发送到服务器,而它在RestKit 0.20中似乎是。 我已经试过明确地设置assignsDefaultValueForMissingAttributesNO ,但似乎没有什么差别。

服务器需要以下格式:

 {"response": {"assessment_id":"1","time_taken":"60"}, "answer": [ {"question_number": 1, "answer_value": 3}, {"question_number": 2, "answer_value": 2}, {"question_number": 3, "answer_value": 1}, ] } 

我已经设置了一个CompletedAssessment对象,它包含一个Response对象和一个Answer对象数组。 (请注意,当从服务器接收到这些对象时,需要接收比需要发送更多的属性)。

 @interface CompletedAssessment : NSObject { Response *response; NSArray *answers; } @interface Answer : NSObject { NSNumber *identifier; NSNumber *responseId; NSNumber *questionNumber; NSString *answerHistory; NSString *answerValue; NSString *answerText; NSNumber *timeTaken; } @interface Response : NSObject { NSNumber *identifier; NSNumber *assessmentId; NSNumber *timeTaken; NSNumber *clientId; NSString *assessmentShortName; NSString *score; NSString *interpretation; NSString *dateCreated; NSString *localTime; 

}

我把这个映射设置如下:

 RKObjectMapping *answerMapping = [RKObjectMapping mappingForClass:[Answer class]]; answerMapping.assignsDefaultValueForMissingAttributes = NO; [answerMapping addAttributeMappingsFromDictionary:@{ @"id": @"identifier", @"response_id": @"responseId", @"question_number": @"questionNumber", @"answer_history": @"answerHistory", @"answer_value": @"answerValue", @"answer_text": @"answerText", @"time_taken": @"timeTaken" }]; RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[Response class]]; responseMapping.assignsDefaultValueForMissingAttributes = NO; [responseMapping addAttributeMappingsFromDictionary:@{ @"id": @"identifier", @"client_id": @"clientId", @"assessment_id": @"assessmentId", @"time_taken": @"timeTaken", @"score": @"score", @"assessment_short_name": @"assessmentShortName", @"interpretation": @"interpretation", @"created": @"dateCreated", @"local_time": @"localTime" }]; RKObjectMapping *completedAssessmentMapping = [RKObjectMapping mappingForClass:[CompletedAssessment class]]; completedAssessmentMapping.assignsDefaultValueForMissingAttributes = NO; [completedAssessmentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"response" toKeyPath:@"response" withMapping:responseMapping]]; [completedAssessmentMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"answer" toKeyPath:@"answers" withMapping:answerMapping]]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:completedAssessmentMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"data.completedAssessment" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor]; RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[completedAssessmentMapping inverseMapping] objectClass:[CompletedAssessment class] rootKeyPath:nil method:RKRequestMethodPOST]; [[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor]; [objectManager.router.routeSet addRoute:[RKRoute routeWithClass:[CompletedAssessment class] pathPattern:@"clients/:response.clientId/responses" method:RKRequestMethodPOST]] ; 

日志logging揭示了JSON结束的格式:

 {"response": {"interpretation":null,"id":null,"score":null,"client_id":15,"local_time":"2015-8-6 13:8:34","time_taken":5,"assessment_short_name":null,"assessment_id":8,"created":null}, "answer":[ {"answer_value":"0","id":null,"answer_text":null,"answer_history":null,"time_taken":null,"response_id":null,"question_number":1}, {"answer_value":"1","id":null,"answer_text":null,"answer_history":null,"time_taken":null,"response_id":null,"question_number":2} ]} 

RestKit日志logging确认空映射:

 restkit.object_mapping:RKMappingOperation.m:873 Mapped relationship object from keyPath 'response' to 'response'. Value: { "assessment_id" = 8; "assessment_short_name" = "<null>"; "client_id" = 15; created = "<null>"; id = "<null>"; interpretation = "<null>"; "local_time" = "2015-8-6 13:8:34"; score = "<null>"; "time_taken" = 5; } restkit.object_mapping:RKMappingOperation.m:715 Mapped attribute value from keyPath 'identifier' to 'id'. Value: (null) ... 

请帮忙!

你正在创build一个新的映射,在这一行中调用[selfCompletedAssessmentMapping inverseMapping]

 RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[selfCompletedAssessmentMapping inverseMapping] objectClass:[CompletedAssessment class] rootKeyPath:nil method:RKRequestMethodPOST]; 

将其保存到一个variables并在创build描述符之前将assignsDefaultValueForMissingAttributesNO

 RKObjectMapping *requestMapping = [selfCompletedAssessmentMapping inverseMapping]; requestMapping.assignsDefaultValueForMissingAttributes = NO; RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[CompletedAssessment class] rootKeyPath:nil method:RKRequestMethodPOST];