在shouldSetValue:atKeyPath restkit中将NSNull值强制转换为nil

时间:2013-09-25 16:47:41

标签: ios restkit restkit-0.20

我正在拥有一个从数据库中收集公司名称和电话号码的网络服务。但有时候不提供电话号码。当我尝试使用restkit将电话号码映射到对象时,我会收到警告,有时候值为零。然后我收到警告信息:

Coercing NSNull value to nil in shouldSetValue:atKeyPath: -- should be fixed.

有关如何将nil映射到NSNull的任何建议吗?

这是我的customerrealtion类(.h)的头文件:

 #import <Foundation/Foundation.h>
 #import <RestKit/RestKit.h>

 @interface CustomerRelation : NSObject

 #pragma private fields


 @property (nonatomic, copy) NSString *companyName;
 @property (nonatomic, copy) NSString *phoneNumber;

 @end

客户关系类(.m)的实施文件

 #import "CustomerRelation.h"

 @implementation CustomerRelation



-(BOOL)ValidatecompanyName:(id*)ioValue error:(NSError **)outError {
   NSLog(@"error");

}
-(BOOL)validatephoneNumber:(id *)ioValue error:(NSError **)outError {
if (*ioValue == nil) {
    if (outError != NULL) {

              }
    return NO;
}
return YES;
}

@end

这是我进行映射的地方:

- (void)viewDidAppear:(BOOL)animated{

RKObjectManager * client = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://127.0.0.1"]];
RKObjectMapping *articleMapping = [RKObjectMapping requestMapping];
[articleMapping addAttributeMappingsFromDictionary:@{@"Companyname": @"companyName",
                                                     @"Phonenumber": @"phoneNumber"}];
RKResponseDescriptor *rkresponsedesc =  [RKResponseDescriptor responseDescriptorWithMapping:articleMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"customers" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[client addResponseDescriptor:rkresponsedesc];

NSMutableURLRequest *request = [client requestWithObject:nil method:RKRequestMethodGET path:@"test.php" parameters:nil];



RKObjectRequestOperation *operation = [client objectRequestOperationWithRequest:request
                                                                               success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                                                   [self mappingSuccess:mappingResult];

                                                                               } failure: ^(RKObjectRequestOperation *operation, NSError *error) {
                                                                                   NSLog(@"Failed with error: %@", [error localizedDescription]);
                                                                               }];
[client enqueueObjectRequestOperation:operation];



MainViewController *viewController =  [self.storyboard instantiateViewControllerWithIdentifier:@"mainViewController"];

[self.navigationController pushViewController:viewController animated:YES];

}

 - (void)mappingSuccess:(RKMappingResult*)mappingResult
{
    NSLog(@"Success block: %@", mappingResult);
}

2 个答案:

答案 0 :(得分:2)

看到该错误,我猜您正在使用旧版本的RestKit并且您应该更新。

那说你应该能够使用RestKit支持的KVC验证。如果在目标模型对象上实现validateValue:forKey:error:,则可以验证并编辑为任何键设置的值。

答案 1 :(得分:0)

我终于找到了为什么从未调用过验证方法的原因。 错误在行

RKObjectMapping *customerRelationMapping = [RKObjectMapping requestMapping];

应该取得什么样的课程:

  RKObjectMapping *customerRelationMapping = [RKObjectMapping mappingForClass:[Article class]];