2013-09-25 20 views
2

我有一個從數據庫中收集公司名稱和電話號碼的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); 
} 

回答

0

我終於找到爲什麼驗證方法從未被調用過。 錯誤是在該行

RKObjectMapping *customerRelationMapping = [RKObjectMapping requestMapping]; 

什麼應該得到的類,而不是:採用最新restkit(0.20)

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

看到這個錯誤我猜你使用的是舊版本的RestKit,應該更新。

這就是說你應該能夠通過KVC驗證來配合RestKit支持的。如果您在目標模型對象上實現了validateValue:forKey:error:,則可以驗證並編輯爲任何鍵設置的值。

+0

IAM。你可以給我一個例子如何實現驗證值:forkey:error:?或者將我指向正確的文檔頁面?我可以在他們的文檔中找到它。 thx –

+0

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

+0

我試圖實現驗證值,但我仍然得到這個警告,如果我設置了一個斷點到validatePhonenumber方法,它不會穿過它。我在原始問題中添加了更多代碼 –