Вопрос

When the user has Pressed Login button i want to display the particular first name in alert view when the user has entered their username and password. Please help me. Thanks in advance. Help will be appreciated.

    - (IBAction)LoginPressed:(id)sender
    { 


     if (managedObjContext == nil) {

    AppDelegate *app = (AppDelegate*)[UIApplication sharedApplication].delegate;


    managedObjContext= app.managedObjectContext;
}

NSFetchRequest *request =[[NSFetchRequest alloc]init];

NSEntityDescription *rocordTableEntity = [NSEntityDescription entityForName:@"RecordTable" inManagedObjectContext:managedObjContext];


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username==% @AND password==%@",L_usernameField.text,L_passwordField.text];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"firstname=%@",[managedObjContext valueForKey:@"firstname"]];

[request setEntity:rocordTableEntity];
[request setPredicate:predicate];
[request setPredicate:pred];

NSError *error;

NSArray *arrarforCheckingUserANDPass = [managedObjContext executeFetchRequest:request       error:&error];
NSString *value =  [arrarforCheckingUserANDPass valueForKey:@"firstname"];



if (arrarforCheckingUserANDPass.count != 0) {




    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Welcome" message:value d elegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];

    [alert show];

}
else{

    NSLog(@"not matched");
}



} 
Это было полезно?

Решение

Your second setPredicate overwrites the first one. You do not need it. You get the whole entity and then simply access the firstName property. Your first predicate has wrong syntax.

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"RecordTable"];
request.predicate = [NSPredicate predicateWithFormat:
     @"username==%@ AND password==%@",L_usernameField.text,L_passwordField.text];

NSArray *result = [self.managedObjectContext executeFetchRequest:request error:nil];
RecordTable *user = result.firstObject;
if (user) {
   NSLog(@"The first name is %@.", user.firstName);
}

BTW, why does your method start with a capital letter? That is a very bad habit. Your entity name is also a disaster. "RecordTable" is about the worst entity name you could choose. If it is a user, call it "User". Following convention, I camel-cased your firstName property as well.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top