문제

I have a Mac application that validates user credentials , Name & PIN. Using CoreData, how do I reference the key from a particular value? For example, in the database, John Doe has a PIN of 1234. When the user chooses their name, they enter their pin and hit the login button. If the pin matches what CoreData has in its database, it allows the user to continue. I have everything set up and ready to go, I just can't figure out how to get the PIN value from Core Data. I will then compare it to the string value entered in by the user to validate the user's credentials. Its probably something simple but I can't figure it out. Entity name: Employee Attributes: 'employeeName' & 'employeePin'.

- (IBAction)loginButton:(NSButton *)sender {
NSString *name = self.chooseNameBox.selectedCell;
NSString *pass = self.pinEntryField.stringValue;

context.???
}

Any help is appreciated.

도움이 되었습니까?

해결책

You need to setup a fetch request like this:

NSError *aError = nil;
NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"Employee" inManagedObjectContext:self.currentManagedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSPredicate* entriesPredicate = [NSPredicate predicateWithFormat:@"employeeName = %@", name];
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

[request setPredicate:entriesPredicate];
[request setSortDescriptors:@[sortDescriptor]];

NSArray *employeeArray = [self.currentManagedObjectContext executeFetchRequest:request error:&aError];

EmployeeObject *retrievedEmployee = [employeeArray lastObject];
NSString *retrievedPin = retrievedEmployee.employeePin;

Also, I agree with @trojanfoe you need to hash the pin before inserting it into the database.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top