我有一個從數據庫中收集公司名稱和電話號碼的Web服務。但有時不提供電話號碼。當我嘗試將電話號碼映射到具有restkit的對象時,我得到的警告值有時爲零。然後我收到警告消息:在shouldSetValue強制NSNull值爲:atKeyPath restkit
Coercing NSNull value to nil in shouldSetValue:atKeyPath: -- should be fixed.
如何將nil映射到NSNull?
這是我customerrealtion類的我的頭文件(.H):客戶關係類(.M)的
#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
實現文件
#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);
}
IAM。你可以給我一個例子如何實現驗證值:forkey:error:?或者將我指向正確的文檔頁面?我可以在他們的文檔中找到它。 thx –
https://github.com/RestKit/RestKit/blob/16eb7ed46ec17721c073ff9ebb14bea58ce4848d/Docs/Object%20Mapping.md#key-value-validation and https://developer.apple.com/library/mac/documentation/cocoa/概念/ KeyValueCoding/Articles/Validation.html#// apple_ref/doc/uid/20002173-CJBDBHCB – Wain
我試圖實現驗證值,但我仍然得到這個警告,如果我設置了一個斷點到validatePhonenumber方法,它不會穿過它。我在原始問題中添加了更多代碼 –