Pregunta

What I am doing to fetch the data for core data is following

NSString *str;
NSPredicate *predicate;

switch (typeCube) {
    case NewCubes: {
        str = [NSString stringWithFormat:@"type == %d",NewCubes];
        break;
    }
    case AllCubes: {
        str = [NSString stringWithFormat:@"type != %d",CustomCubes];
        break;
    }
    case CustomCubes: {
        str = [NSString stringWithFormat:@"type == %d",CustomCubes];
        break;

    }
}

predicate = [NSPredicate predicateWithFormat:@"%@ AND (accounts.keyChainId == %@)", str, accountKeyChainId];

However, the result of predicate is nil. But the following works

predicate = [NSPredicate predicateWithFormat:@"(type == %d) AND (accounts.keyChainId == %@)", type, accountKeyChainId]; ( type is either NewCubes or AllCubes or CustomCubes)

Please help if you have any ideas. All comments are welcomed. Thanks

¿Fue útil?

Solución

There is a class called NSCompoundPredicate which allows you to construct predicates with AND, OR and NOT operators. Here you need

[NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate2, etc.]];

So the code will be --

NSPredicate *predicate1;
NSPredicate *predicate;

    switch (typeCube) {
        case NewCubes: {
            predicate1 = [NSPredicate predicateWithFormat:@"type == %d",NewCubes];
            break;
        }
        case AllCubes: {
            predicate1 = [NSPredicate predicateWithFormat:@"type != %d",CustomCubes];
            break;
        }
        case CustomCubes: {
            predicate1 = [NSPredicate predicateWithFormat:@"type == %d",CustomCubes];
            break;

        }
    }
    predicate = [NSPredicate predicateWithFormat:@"accounts.keyChainId == %@", accountKeyChainId];
    [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate]];

Read more about NSCompountPredicates here: http://nshipster.com/nspredicate/

Otros consejos

Creating a predicate with a format will insert various quotes and changes to the supplied parameters such that it is valid and suitable for use. By supplying part of the predicate as a parameter you are getting tangled up with this feature.

Change your switch statement to create the full format string, but not insert any parameters. Then, create the predicate with that format string and the set of required parameters:

NSString *format;
id cubesParameter;

switch (typeCube) {
    case NewCubes: {
        format = @"type == %d AND (accounts.keyChainId == %@)";
        cubesParameter = NewCubes;
        break;
    }
    case AllCubes: {
        format = @"type != %d AND (accounts.keyChainId == %@)";
        cubesParameter = CustomCubes;
        break;
    }
    case CustomCubes: {
        format = @"type == %d AND (accounts.keyChainId == %@)";
        cubesParameter = CustomCubes;
        break;
    }
}

NSPredicate *predicate = [NSPredicate predicateWithFormat:format, cubesParameter, accountKeyChainId];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top