简体   繁体   中英

compare coredata with string

in objective ci cant compare my coredata with a string

- (IBAction)loginBut:(id)sender {
    AppDelegate *appDel = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDel managedObjectContext];
    /*NSEntityDescription *newPassword = [NSEntityDescription insertNewObjectForEntityForName:@"Pinpass" inManagedObjectContext:context];
    //[newPassword setValue:@"9090" forKey:@"password"];
    //[context save:nil];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Pinpass"];
    request.returnsObjectsAsFaults = NO;
    NSArray *result = [context executeFetchRequest:request error:nil];
    NSLog(@"%@", result);*/
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Pinpass"];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(password = %@)", loginField.text];
    [request setPredicate:pred];
    NSError *error;
    NSArray *result = [context executeFetchRequest:request error:&error]; ;


    if (result.count > 0){

        //If([[textField text] compare:entity.attribute]==NSOrderedSame) NSLog(@"Correct!");
                if ([[loginField text] isEqual:[result valueForKey:@"password"]])
 {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sign in ok"
                                                        message:@"Sign in succsess!!"
                                                        delegate:self
                                                        cancelButtonTitle:@"OK"
                                                        otherButtonTitles:nil];
                                                        [alert show];
        }
        else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sign in ok"
                                                            message:@"Sign in succsess!!"
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Say Hello",nil];  }

    }

is something wrong with my code?? or is just a bad use of functions

if ([[loginField text] isEqual:[result valueForKey:@"password"]])

result is an array so you need to specify an index and then check the value at that index.

For example, the line above should be something like

if ([[loginField text] isEqual:[[result valueForKey:@"password"] lastObject]])

There are several things here:

  1. Your predicate is checking for managed object with the attribute password equal to loginField.text , so any match automatically is the correct password by definition.
  2. Your result is a NSArray . Presumably you want to compare your textField text to a single object in that array. Something like this:

if (result.count > 0) { firstResult = [result firstObject]; if ([[loginField text] isEqual:[firstResult valueForKey:@"password"]]) { do stuff... } }

Result array contains NSManagedObject name Pinpass. So you need to get the value from it and compare...

if (result.count > 0){
Pinpass *pinObj=[result objectAtIndex:0];

if ([[loginField text] isEqual:[pinObj valueForKey:@"password"]])
 {
 //AlertView
 }
 else
 { 
 //AlertView
 }

}

Hope it helps you..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM