hello I am working on swift. I need to know how can I find results which matches the exact string. Here's my code
let userID: String = String(sender.tag)
// Create a Predicate with mapping to trip_id
let filterByRequest: NSPredicate = NSPredicate(format: "%K.%K CONTAINS[c] %#", "ProductRequest", "user_id", userID)
// Filter your main array with predicate, resulting array will have filtered objects
let filteredArray: [AnyObject] = self.array.filteredArrayUsingPredicate(filterByRequest)
The problem is If user id is 69 it shows results of users whose id is 69, 6, and 9.
I googled but I find some answers closed to my question but they were all in objective C.
Use MATCHES in predicate as following :
let filterByRequest: NSPredicate = NSPredicate(format: "%K.%K MATCHES %#", "ProductRequest", "user_id", userID)
Hope it helps..
To test for exact equality, simply use == instead of CONTAINS:
NSPredicate(format: "%K.%K == %#", ...)
This can also be combined with [c] for case-insensitive equality:
NSPredicate(format: "%K.%K ==[c] %#", ...)
Related
I have an array of strings as below:
["Milk","Milkshake","Milk Shake","MilkCream","Milk-Cream"]
and if I search for "milk" then results should be ["Milk","Milk Shake","Milk-Cream"] i.e. search by words.
With the predicate as
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"tagName CONTAINS[c] %#",containSearchTerm];
I am getting all the results from above array. How can I perform match using words ?
You need a “regular expression search” in order to match against word boundaries, that is done with "MATCHES" in a predicate. Here is an example (essentially translated from NSPredicate with core data, search word with boundaries in string attribute to Swift):
let searchTerm = "milk"
let pattern = ".*\\b\(NSRegularExpression.escapedPattern(for: searchTerm))\\b.*"
let predicate = NSPredicate(format: "tagName MATCHES[c] %#", pattern)
This searches for all entries where the tagName contains the given search term, surrounded by a “word boundary” (the \b pattern).
I have a UITableView with a UISearchbar that let me filter the data based on the search text from the UISearchbar.
The CoreData table contains 3 attributes
name, notes, date
I want to search the three columns for any occurrence based on the User search text.
So I tried this on:
let searchText = searchText.lowercased()
let query = "name contains[cd] %# OR notes contains[cd] %# OR date contains[cd] %#"
let predicate = NSPredicate(format: query, searchText, searchText, searchText)
Is there any way to pass the same parameter (searchText) one time?
Something like Java string formatter:
let query = "name contains[cd] %1$# OR notes contains[cd] %1$# OR date contains[cd] %1$#"
let predicate = NSPredicate(format: query, searchText)
You can use substitution variables:
let searchText = searchText.lowercased()
let template = NSPredicate(format: "name contains[cd] $SRCH OR notes contains[cd] $SRCH OR date contains[cd] $SRCH")
let subVars = ["SRCH": searchText]
let predicate = template.withSubstitutionVariables(subVars)
See "Creating Predicates using Predicate Templates" in the Apple Documentation.
after migration to swift3, I have an issue that cannot fix
let fetchRequest: NSFetchRequest<User> = User.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "id == %#", id)
my App crashes on second line, bad access, no reason. types are right, no log, nothing, just bad access. any suggestions?
Found a reason, predicate is wrong, cause id is Int64 type, have no idea what kind of predicate I need for this version of swift
The %# format expect a Foundation object as argument, compare
"Predicate Format String Syntax" in the "Predicate Programming Guide".
You can bridge the Int64 to NSNumber:
let id = Int64.max
let predicate = NSPredicate(format: "id == %#", id as NSNumber)
print(predicate) // id == 9223372036854775807
or change the format to "long long":
let id = Int64.max
let predicate = NSPredicate(format: "id == %lld", id)
print(predicate) // id == 9223372036854775807
Bridging all number types to NSNumber is possible as of Swift 3.0.1 (Xcode 8.1) with the implementation of
SE-0139 Bridge Numeric Types to NSNumber and Cocoa Structs to NSValue.
I want to show all items where value1 contains value2. I tried this:
let fetchRequest = NSFetchRequest(entityName: "Product")
fetchRequest.predicate = NSPredicate(format: "value1 CONTAINS[cd] value2")
value1, value2 - current object values, it is not variables
But i got error:
Unable to parse the format string
Why it doesn't allow me to do this ?
Try to use this predicate:
let predicate = NSPredicate(format: "value1 CONTAINS[cd] %#", value2)
As were investigated during communication with developer. Issue is in data that is saved to the database. In his case data is saved with quotes ("") and NSPredicate(format: "value1 CONTAINS[cd] %#", value2) is working with errors due to that issue.
I've got a single string substitution working with NSPredicate but returning core data records that contain either StringA or StringB doesn't seem to be something I can figure out. I want something like this:
let filter = NSPredicate(format: ("%K = %#", "type", "StringA") OR ("%K = %#", "type", "StringB"))
But of course that doesn't work. Help?
You have to specify a format string,
followed by a comma-separated list of arguments to substitute into the format:
let filter = NSPredicate(format:"%K = %# OR %K = %#", "type", "StringA", "type", "StringB")
If the keys are not reserved words and do not contain special characters then you
can specify them directly in the format string:
let filter = NSPredicate(format:"type = %# OR type = %#", "StringA", "StringB")