gpt4 book ai didi

core-data - 自定义验证期间Magical Record Core数据中的上下文保存问题

转载 作者:行者123 更新时间:2023-12-02 03:32:22 25 4
gpt4 key购买 nike

我创建了一个名为“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”和“password”上,例如:
- (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注册时,我不需要输入密码并确认密码字段。在不保存“password”和“confirmPassword”值的情况下该如何保存上下文?

最佳答案

看起来很简单:

  • 您的Person类包含passwordconfirmPassword字段的验证方法。这些方法不接受nil值。
  • 您创建了Person的实例,但未为password设置值。因此,新实例对此字段的值为nil。
  • 您尝试保存更改。

  • 简而言之, 验证失败,因为您自己的验证代码要求失败。为使验证通过,您必须为 password分配一个有效值(其中“有效”表示您的代码将接受的值)或更改验证代码,以使nil值通过验证。

    目前尚不清楚Facebook与这个问题有什么关系。您的代码中似乎没有任何内容与Facebook相关。

    关于core-data - 自定义验证期间Magical Record Core数据中的上下文保存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862671/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com