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