Filter with Nspredicate with inheritance

Asked

Viewed 72 times

1

I have a structure in Core Data thus:

Cliente
--- id = inteiro
--- dados = Pessoa

Pessoa
--- nome = string

PessoaFisica < Pessoa
--- cpf = string

PessoaJuridica < Pessoa
--- cnpj = string

How can I filter Client based on documents depending on the type of person? For example, seeking a customer according to the Cpf.

If I do it like this, he doesn’t understand the inheritance and says he didn’t find it Cpf in Person:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dados.cpf = %@", cpf];

I needed a kind of "casting" of dice for Personal. Any suggestions?

1 answer

1


I suggest using the block system:

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    Cliente *cliente = evaluatedObject;
    PessoaFisica *pessoa = cliente.dados;
    return [pessoa isKindOfClass:[PessoaFisica class]] && [pessoa.cpf isEqualToString:cpf];
}];

The trick is in [pessoa isKindOfClass:[PessoaFisica class]], returning YES Point pessoa be an instance of PessoaFisica.

There is also the method isMemberOfClass:. The difference between the two is that isMemberOfClass: only returns YES if pessoa really be the type PessoaFisica, if it is a sub class will already return NO. While isKindOfClass: returns YES if pessoa be the type PessoaFisica or of a sub-class.

I hope it helps ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.