Form NSPredicate from string that contains id's
The "MATCHES" operator of NSPredicate
can be used to compare against a regular expression:
NSString *pageId = @"1";
NSString *regex = [NSString stringWithFormat:@"(.*,)?%@(,.*)?", pageId];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pages MATCHES %@", regex];
But you should also consider to replace the string property by a to-many relationship to a Page entity, then the predicate would look like
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY pages.pageid == %@", pageId];
onFormat string For NSPredicate
Your predicte should be like this
let predicate = NSPredicate(format: "id IN %@", objectIDs)
let sortByUserId = NSSortDescriptor(key: "id", ascending: true)
Note : It is either self.id IN %@
or id IN %@
.
NSPredicate query for not containing a specific string
Your first predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", sessionID];
should work to find all records where the attribute listingID
is not equal to sessionID
(provided that listingID
and sessionID
have the same type).
If both are strings and you want to find all records where listingID
does not contain the string sessionID
as a substring, then this predicate should work:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (listingID CONTAINS %@)", sessionID];
Use "CONTAINS[cd]" if the string comparison should be done case and diacritical insensitive.
NOTE: You can specify the attribute name as an argument, but then you must use %K
instead of %@
as format:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS %@)", @"listingID", sessionID];
Fetch from core data where value contains string
For string comparisons, you could use LIKE instead of ==, and also, for your bool, use a literal, @YES, so your predicate would be :
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"userid == %@ AND objectids LIKE %@ AND active == %@", user.userid, objectid, @YES];
For more complex string comparisons, you could use a predicateWithBlock:
EDIT : reading your further comments, I think this question answers the exact same problem using regex : Form NSPredicate from string that contains id's
How to get all items with NSPredicate CONTAINS IN array
You almost had it :)
NSArray *objects = @[
@{
@"itemID" : @1
},
@{
@"itemID" : @2
},
@{
@"itemID" : @3
},
@{
@"itemID" : @4
},
@{
@"itemID" : @5
}
];
NSArray *idsToLookFor = @[@3, @4];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"itemID IN %@", idsToLookFor];
NSArray *result = [objects filteredArrayUsingPredicate:predicate];
NSLog(@"result: %@", result);
And if you do not want to pass in any array, but write the predicate "in hand", the syntax would be:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"itemID IN { 3, 4 }"];
And the result will be:
result: (
{
itemID = 3;
},
{
itemID = 4;
}
)
Core Data NSPredicate: Return all objects which are related to another object whos ID appears in a given array
Try adding an @count
to your subquery.
NSPredicate(format: "SUBQUERY(events, $event, ANY $event.clientID in %@).@count != 0", clientIDs as CVarArg)
Checkout these docs for bit of a reference. It is for NSExpression but the "Discussion" section has an example of a subquery.
Related Topics
Swift - Weekday by Current Location by Currentcalendar()
Where Is Uiview.Init() Documented
Swift (Beta 3) "Nsdictionary? Does Not Conform to Protocol 'Equatable'"
Uipageviewcontroller Setviewcontrollers, UIpagecontrol Not Showing Right Current Number
Mkannotation Not Getting Selected in iOS5
Start Position of Table View Is Wrong in Simulator, and Is Not The Same in Storyboard
How to Print Name of The Day of The Week
Mpvolumeview Does Not Show Route Button on Launch
Why [Uiscreen Mainscreen].Bounds] Is Not Returning Full Screen Size
Paste Formatted Text, Not Images or HTML
How to Schedule Mail Using Gmail API
How to Set UIactivityviewcontroller Gmail Share Subject Different Than Body
Firebase Database Not Equal Request - Alternative Solution (For iOS)
Conversion from String to Date in Swift Returns Nil