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);
}
答案 0 :(得分:2)
看到该错误,我猜您正在使用旧版本的RestKit并且您应该更新。
那说你应该能够使用RestKit支持的KVC验证。如果在目标模型对象上实现validateValue:forKey:error:
,则可以验证并编辑为任何键设置的值。
答案 1 :(得分:0)
我终于找到了为什么从未调用过验证方法的原因。 错误在行
RKObjectMapping *customerRelationMapping = [RKObjectMapping requestMapping];
应该取得什么样的课程:
RKObjectMapping *customerRelationMapping = [RKObjectMapping mappingForClass:[Article class]];