2

我有一个从数据库中收集公司名称和电话号码的网络服务。但有时没有提供电话号码。当我尝试使用 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);
}
4

2 回答 2

2

看到那个错误我猜你正在使用旧版本的 RestKit,你应该更新。

也就是说,您应该能够将这一点与 RestKit 支持的 KVC 验证联系起来。如果您validateValue:forKey:error:在目标模型对象上实现,您可以验证和编辑为任何键设置的值。

于 2013-09-25T17:40:52.810 回答
0

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

RKObjectMapping *customerRelationMapping = [RKObjectMapping requestMapping];

什么应该得到这个类:

  RKObjectMapping *customerRelationMapping = [RKObjectMapping mappingForClass:[Article class]];
于 2013-10-13T12:48:13.063 回答