Inherited NSPredicate Filter

1

I have a structure in Core Data like this:

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

Pessoa
--- nome = string

PessoaFisica < Pessoa
--- cpf = string

PessoaJuridica < Pessoa
--- cnpj = string

How can I filter Customer based on the documents depending on the type of person? For example, fetch a client according to cpf .

If I do this right, he does not understand the inheritance and says he did not find cpf in Person :

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

I needed some sort of casting of data for Person . Any suggestions?

    
asked by anonymous 29.05.2014 / 16:12

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 [pessoa isKindOfClass:[PessoaFisica class]] , which returns YES casso pessoa is an instance of PessoaFisica .

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

I hope it helps;)

    
02.06.2014 / 17:27