gpt4 book ai didi

ios - 核心数据获取概念

转载 作者:行者123 更新时间:2023-11-29 01:27:16 26 4
gpt4 key购买 nike

你好,我正在创建一个示例项目,其中我有登录和注册类。我创建了一个包含所有文本字段的注册类,并使用 coredata 成功注册了它。问题是我需要让注册用户进入登录类,以便它成功进入下一页。我必须检查用户是否存在于我的登录类中。

注册.m

@implementation CoreSignup

@synthesize managesObjectContext=_managesObjectContext;
@synthesize managesObjectModel=_managesObjectModel;
@synthesize persistentStoreCoordinator=_persistentStoreCoordinator;


#pragma mark Insert Row In CoreData

-(void)insertSignUpList:(NSMutableDictionary *)details_Ary
{
BOOL isInserted=[self insertRowForSignUp:[details_Ary valueForKey:@"Firstname"] Lastname:[details_Ary valueForKey:@"Lastname"] password:[details_Ary valueForKey:@"Password"] emailid:[details_Ary valueForKey:@"Emailid"] phoneNo:[details_Ary valueForKey:@"Phoneno"] city:[details_Ary valueForKey:@"City"]];

if (isInserted)
{
//inserted Successfully

NSLog(@"inserted Successfully");
}
}

-(BOOL)insertRowForSignUp:(NSString *)firstName Lastname:(NSString *)LastName password:(NSString *)password emailid:(NSString *)emailid phoneNo:(NSString *)phoneNo city:(NSString *)city{

AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =
[appDelegate managedObjectContext];

SignUp *signUpInfo=[NSEntityDescription insertNewObjectForEntityForName:@"SignUp" inManagedObjectContext:context];
signUpInfo.firstName=firstName;
signUpInfo.lastName=LastName;
signUpInfo.password=password;
signUpInfo.phoneno=@([phoneNo intValue]);
signUpInfo.city=city;
signUpInfo.emailid=emailid;

NSError *error;
if (![context save:&error]) {

NSLog(@"Oops coudnt save");
return NO;
}

return YES;
}

-(NSMutableArray*)fetchAll{

AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =
[appDelegate managedObjectContext];

NSFetchRequest *request=[[NSFetchRequest alloc]init];
NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
[request setEntity:entity1];

NSArray *emptyArray=[self.managesObjectContext executeFetchRequest:request error:nil];
NSMutableArray *AllHistory=[NSMutableArray new];

for (SignUp*signUpHistory in emptyArray) {

NSMutableDictionary *tempDict=[[NSMutableDictionary alloc]init];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.firstName] forKey:@"firstname"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.lastName] forKey:@"lastName"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.password] forKey:@"password"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.emailid] forKey:@"emailid"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.city] forKey:@"city"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.phoneno] forKey:@"phoneno"];
[AllHistory addObject:tempDict];

}
return AllHistory;
}

登录.m

@interface ViewController ()<UITextFieldDelegate>

@end

@implementation ViewController
@synthesize JJuser;
@synthesize JJpassword;
@synthesize managesObjectContext=_managesObjectContext;
@synthesize managesObjectModel=_managesObjectModel;
@synthesize persistentStoreCoordinator=_persistentStoreCoordinator;


- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self jjtext];
[self jjpass];
}

-(void)jjtext{
JJuser.delegate=self;
[JJuser enableMaterialPlaceHolder:YES];
}

-(void)jjpass{
JJpassword.delegate=self;
[JJpassword enableMaterialPlaceHolder:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(NSMutableArray*)fetchLogin{

AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =
[appDelegate managedObjectContext];

NSFetchRequest *request=[[NSFetchRequest alloc]init];
NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
[request setEntity:entity1];

NSArray *emptyArray=[self.managesObjectContext executeFetchRequest:request error:nil];
NSMutableArray *AllHistory=[NSMutableArray new];

for (SignUp*signUpHistory in emptyArray) {

NSMutableDictionary *tempDict=[[NSMutableDictionary alloc]init];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.firstName] forKey:@"firstname"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.lastName] forKey:@"lastName"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.password] forKey:@"password"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.emailid] forKey:@"emailid"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.city] forKey:@"city"];
[tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.phoneno] forKey:@"phoneno"];
[AllHistory addObject:tempDict];

}
return AllHistory;
}



- (IBAction)Login:(id)sender {

AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =
[appDelegate managedObjectContext];

NSFetchRequest *FindIt=[[NSFetchRequest alloc]init];
NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
[FindIt setEntity:entity1];

NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"firstName = \"%@\"",FindIt]];
[FindIt setPredicate:pred];

NSUInteger count = [context countForFetchRequest:FindIt error:nil];
if (count == NSNotFound)
{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"Hello" message:@"No Such id exist" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];

}else if (count == 1)
{

}
else{


}

}

- (IBAction)Signup:(id)sender
{
UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SignUpViewController*SignUp = [board instantiateViewControllerWithIdentifier:@"SignUp"];
[self.navigationController pushViewController:SignUp animated:YES];
}
@end

最佳答案

您可以使用获取请求来检查用户是否已经存在,如果存在则密码是否匹配

试试这个,告诉我它是否有效

-(BOOL)searchDatabaseForUser:(NSString *)username andPassword:(NSString *)password{

AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"EntityName"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username = %@",username];
[fetchRequest setPredicate:predicate];

NSError *error = nil;


NSArray *result = [context executeFetchRequest:fetchRequest error:&error];

if([result count]){
for (NSManagedObject *obj in result) {
if([[obj valueForKey:@"password"]isEqualToString:password]){
return YES;
}
}

return NO;
//passwords doesnt match even though username found
}
else {
return NO;
// No such Username found
}
}

现在调用此方法来验证用户是否应该进入下一个屏幕,就像这样

Login.m

-(IBAction)someButtonToCheckLogin:(id)sender{

BOOL shouldUserLogin = [self searchDatabaseForUser:UserNameTextField.txt andPassword:passwordTextField.txt];
if(shouldUserLogin){
//navigate to the next screen.
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title of the application" message:@"Password mismatch, Please check again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];

}

我想这很容易解释。希望这有助于您理解。

关于ios - 核心数据获取概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33865474/

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