ios - Form NSPredicate from string that contains id's -
i have problem can't think of how write predicate.
i have entity called contact, has string property "pages", let's say
contact.pages = @"1,5,11,15,17";
there lot of contacts in database, , want fetch these contacts contains id. let's want fetch these contacts, pages contains id @"1".
i can think of this,
nspredicate* predicate = [nspredicate predicatewithformat:@"self.pages contains %@", _pageid];
but problem contacts, e.g has pages = @"11,15".
so ideas on how accomplish that? grateful suggestions, in advance!
the "matches" operator of nspredicate
can used compare against regular expression:
nsstring *pageid = @"1"; nsstring *regex = [nsstring stringwithformat:@"(.*,)?%@(,.*)?", pageid]; nspredicate *predicate = [nspredicate predicatewithformat:@"pages matches %@", regex];
but should consider replace string property to-many relationship page entity, predicate like
nspredicate *predicate = [nspredicate predicatewithformat:@"any pages.pageid == %@", pageid];
ios objective-c core-data nspredicate nsfetchrequest
No comments:
Post a Comment