我创建了一个名为" Person"的实体。 以下是实体的属性。
@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * confirmPassword;
@property (nonatomic, retain) NSString * createdOn;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * fbId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSString * phNumber;
@property (nonatomic, retain) NSString * sessionToken;
自定义验证添加了两个属性" confirmPassword"和#34;密码"像:
- (BOOL)validatePassword:(id *)ioValue error:(NSError **)outError {
// Password's validation is not specified in the model editor, it's specified here.
// field width: min 4, max 32
BOOL isValid = YES;
NSString *password = *ioValue;
NSString *errorMessage;
NSInteger code = 0;
if (password.length == 0) {
errorMessage = @"Please enter password.";
code = NSValidationMissingMandatoryPropertyError;
isValid = NO;
} else if (password.length < 4) {
errorMessage = @"Password can't be less than 4 characters.";
code = NSValidationStringTooLongError;
isValid = NO;
} else if (password.length > 32) {
errorMessage = @"Password can't be more than 32 characters.";
code = NSValidationStringTooLongError;
isValid = NO;
}
if (outError && errorMessage) {
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : errorMessage };
NSError *error = [[NSError alloc] initWithDomain:kHAB
code:code
userInfo:userInfo];
*outError = error;
}
return isValid;
}
- (BOOL)validateConfirmPassword:(id *)ioValue error:(NSError **)outError {
// Confirm Password's validation is not specified in the model editor, it's specified here.
// field validation
BOOL isValid = YES;
NSString *confirmPassword = *ioValue;
NSString *errorMessage;
NSInteger code = 0;
if (![confirmPassword isEqualToString:self.password]) {
errorMessage = @"The passwords must match";
code = NSValidationStringPatternMatchingError;
isValid = NO;
}
if (outError && errorMessage) {
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : errorMessage };
NSError *error = [[NSError alloc] initWithDomain:kHAB
code:code
userInfo:userInfo];
*outError = error;
}
return isValid;
}
值将保存在Person实体中,如:
Person *userProfile = [Person MR_createEntity];
NSString *facebookId = @"some id";
[userProfile setFbId:facebookId];
[userProfile setEmail:@"umairsuraj.engineer@gmail.com"];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
无法保存上下文,说人员实体未通过有效的密码验证并确认密码字段。从Facebook注册时我无需输入密码和确认密码字段。我该怎么做才能保存上下文而不保存&#34;密码&#34;和&#34; confirmPassword&#34;。?
答案 0 :(得分:1)
看起来非常简单:
Person
包含password
和confirmPassword
字段的验证方法。这些方法不接受nil值。 Person
的实例,但没有为password
设置值。因此,新实例的字段值为零。简而言之,验证失败,因为您自己的验证码要求失败。要验证通过,您必须为password
分配有效值(其中“有效”表示您的代码将接受的内容)或更改验证代码,以便nil值通过验证。
目前还不清楚Facebook与这个问题有什么关系。您的代码中的任何内容都不会以任何方式与Facebook相关。