我有这门课:
@interface MovieStatus : NSObject @property (nonatomic, strong) NSNumber* seen; @property (nonatomic, strong) NSNumber* watchlist; @end
其中两个属性都表示可选的可空布尔值.我通过使用RestKit将此对象发送到服务器RKObjectManager
并创建了适当的映射.但在序列化对象时,我无法从POST数据中跳过该属性.
例如,这段代码:
RKLogConfigureByName("*", RKLogLevelTrace); RKObjectManager* manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://www.example.com/v1"]]; manager.requestSerializationMIMEType = RKMIMETypeJSON; RKObjectMapping* requestMapping = [RKObjectMapping requestMapping]; [requestMapping addAttributeMappingsFromArray:@[@"seen", @"watchlist"]]; RKRequestDescriptor* requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[MovieStatus class] rootKeyPath:nil method:RKRequestMethodPOST]; [manager addRequestDescriptor:requestDescriptor]; RKRoute* route = [RKRoute routeWithClass:[MovieStatus class] pathPattern:@"status" method:RKRequestMethodPOST]; [manager.router.routeSet addRoute:route]; MovieStatus* status = [[MovieStatus alloc] init]; status.seen = @(YES); [manager postObject:status path:nil parameters:nil success:nil failure:nil];
正在发送JSON:
{ "seen": true, "watchlist": null }
我想发送的地方:
{ "seen": true }
任何人都可以指出我如何实现它?
我通过将assignsDefaultValueForMissingAttributes
映射设置为NO
:
requestMapping.assignsDefaultValueForMissingAttributes = NO;
现在JSON请求不包含null
值.