¿Cómo verificar la contraseña de un nombre de usuario determinado en Core Data? [cerrado]

I have got a username [string] and I want to check the password for the given username. How would I do that ?

preguntado el 30 de enero de 12 a las 19:01

2 Respuestas

NSString *username = <#Username#>;
NSManagedObjectContext *context = <#Context#>;

NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
fetchRequest.entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context]
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"self.name == %@", username];
fetchRequest.fetchLimit = 1;

NSError *error = nil;
NSArray *users = [context executeFetchRequest:fetchRequest error:&error];

if (users.count > 0)
{
    NSString *password = [[users objectAtIndex:0] valueForKey:@"password"];
}
else if (error != nil)
{
    NSLog(@"User with given username does not exist.");
}
else
{
    NSLog(@"Error fetching user: %@", error);
}

Respondido el 31 de enero de 12 a las 04:01

To query core data for the password of a given username in your application you will need to use a predicate when executing your query. This example assumes that your Entity is named Users and the username you are looking for is named MyUser.

NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Users"
                                              inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *theRequest = [NSFetchRequest new];
[theRequest setEntity:entityDesc];
[theRequest setPredicate:[NSPredicate predicateWithFormat:@"username == %@",@"MyUser"]];
NSError *error;
NSArray *returnResults = [self.managedObjectContext executeFetchRequest:theRequest error:&error];
if (returnResults.count > 0) {
    NSManagedObject *MyUser = [returnResults objectAtIndex:0];
    NSString *password = [MyUser valueForKey:@"password"];
} else if (error != nil) {
    //Error
} else {
    //Error
}

Respondido el 31 de enero de 12 a las 00:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.