gpt4 book ai didi

ios - 无法将对象保存到 CoreData

转载 作者:行者123 更新时间:2023-11-29 12:13:22 25 4
gpt4 key购买 nike

我有一个数据模型,其实体为 HYUser,属性为 firstNamelastNameemail性别电话。当我检查我的 .sqlite 数据库时,那里什么也没有。我已经附上了应该保存我的对象的整个按钮操作。

- (IBAction)btnUserDataSave:(id)sender {
NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@"HYUser" inManagedObjectContext:context];
NSManagedObject *newUser = [[NSManagedObject alloc]initWithEntity:entityDescription insertIntoManagedObjectContext:context];
NSString *gender = NULL;
NSError *error = nil;

[newUser setValue:self.txtfFirstName.text forKey:@"firstName"];
[newUser setValue:self.txtfLastName.text forKey:@"lastName"];
[newUser setValue:self.txtfPhoneNumber.text forKey:@"phone"];
[newUser setValue:self.txtfUserEmail.text forKey:@"email"];

if (self.segmentControlGender.selectedSegmentIndex == 0) {
gender = @"Male";
}else if(self.segmentControlGender.selectedSegmentIndex == 1){
gender = @"Female";
}else{
NSLog(@"ERROR in gender saving");
}
[newUser setValue:gender forKey:@"gender"];

if ([self.txtfFirstName.text isEqual: @""] || [self.txtfLastName.text isEqual: @""] || [self.txtfPhoneNumber.text isEqual: @""] || [self.txtfUserEmail.text isEqual: @""]) {
UIAlertController* errorPrompt = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Required Fields", @"Required Fields")
message:NSLocalizedString(@"All fields are required to complete Sign-up. ", nil)
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK",nil)
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

}];
[errorPrompt addAction:okAction];
[self presentViewController:errorPrompt animated:YES completion:NULL];

}else{
if(![context save:&error]) {
self.labelStatus.text = @"Error";
NSLog(@"%@, %@", error, error.localizedDescription);
}else{
[context save:&error];
self.labelStatus.text = @"Saved";

}
}
}

最佳答案

我解决了这个问题,看起来它与我尝试保存对象的方式有关。以下是我注册新用户的完整代码。

- (IBAction)btnUserDataSave:(id)sender {


HYUser* signUpUserObject = [NSEntityDescription insertNewObjectForEntityForName:@"HYUser" inManagedObjectContext:context];

NSString *gender = nil;
NSError *error = nil;

if ([self.txtfFirstName.text isEqual: @""] || [self.txtfLastName.text isEqual: @""] || [self.txtfPhoneNumber.text isEqual: @""] || [self.txtfUserEmail.text isEqual: @""]) {
UIAlertController* errorPrompt = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Required Fields", @"Required Fields")
message:NSLocalizedString(@"All fields are required to complete Sign-up. ", nil)
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK",nil)
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

}];
[errorPrompt addAction:okAction];
[self presentViewController:errorPrompt animated:YES completion:NULL];

}
else {


self.labelStatus.text = @"Saved";
[signUpUserObject setValue:self.txtfFirstName.text forKey:@"firstName"];
[signUpUserObject setValue:self.txtfLastName.text forKey:@"lastName"];
[signUpUserObject setValue:self.txtfPhoneNumber.text forKey:@"phone"];
[signUpUserObject setValue:self.txtfUserEmail.text forKey:@"email"];

gender = self.segmentControlGender.selectedSegmentIndex == 0 ? @"Male" : @"Female";

[signUpUserObject setValue:gender forKey:@"gender"];

if(![context save:&error]) {
self.labelStatus.text = @"Error";
NSLog(@"%@, %@", error, error.localizedDescription);
}
else {
UIAlertController* errorPrompt = [UIAlertController alertControllerWithTitle:@"HairYourWay"
message:NSLocalizedString(@"STR_NEW_USER_SIGN_UP_SUCCEEDED", @"Success")
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK",nil)
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.navigationController popViewControllerAnimated:YES];
}];
[errorPrompt addAction:okAction];
[self presentViewController:errorPrompt animated:YES completion:NULL];

}
}
}

关于ios - 无法将对象保存到 CoreData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32660228/

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