NSPredicate结果给NSArray

时间:2014-03-23 15:34:07

标签: ios nsarray uitableview nspredicate

我想将谓词的结果放入一个数组中,然后将其放在表格视图中,但我对此仍然是新的。

- (IBAction)search:(id)sender
{
    NSFetchRequest *request = [[NSFetchRequest alloc]init];

    NSEntityDescription *entidesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
    [request setEntity:entidesc];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nombre like %@ and apellido like %@",self.nombretext.text, self.apellidotext.text];
    [request setPredicate:predicate];

    NSError *error;
    NSArray *matchingdata = [context executeFetchRequest:request error: &error];

    if(matchingdata.count == 0) {
        self.display.text = @"Persona no encontrada";
    } else {
        NSString *nombre;
        NSString *apellido;
        int count =0;

        for(NSManagedObject *obj in matchingdata)
        {
            count++;
            nombre =   [obj valueForKey:@"nombre"];
            apellido = [obj valueForKey:@"apellido"];
        }

        if(count == 1)
        {
            self.display.text = [NSString stringWithFormat:@"Nombre: %@,  Apellido: %@", nombre, apellido];
        } else {
            self.display.text = @"Mas de una persona con ese nombre o apellido, intente otra vez";}
        }
    }
}

0 个答案:

没有答案