My Entity Office have set of File entities. Each File has uuid property. I need to fetch offices where we have any file where UUID is not in set of Strings
I tried to use
let predicate = NSPredicate(format: "NOT (ALL files.uuid IN %#)", uuidSet)
let predicate = NSPredicate(format: "ANY files.uuid NOT IN %#", uuidSet)
let predicate = NSPredicate(format: "ANY NOT files.uuid IN %#", uuidSet)
I always get the error like
exception 'NSInvalidArgumentException', reason: 'Unsupported predicate NOT ALL files.uuid IN {"C0DF0E67-ED8A-4D3B-87A1-E4E7B86967AA"}'
Is there a way to write this Predicate properly? Because right now I only see the solution to load all offices and then iterate through every item.
Related
I would like to make predicate more flexible for changes. I have lot of such strings in the sources code:
let predicate = NSPredicate(format: "bodyPart.name = %#", name)
let filteredExercises = ExerciseEntity.mr_findAll(with: predicate)
but as you see if I will change BodyPart name to MuscleGroup for example someday it will cause issue like:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath bodyPart.name not found in entity <NSSQLEntity ExerciseEntity id=5>'
I am searching for the solution with a key paths like
let predicate = NSPredicate(format: "%# = %#", \BodyPart.name, name)
Use %K to substitute the key path
let predicate = NSPredicate(format: "%K = %#", #keyPath(BodyPart.name), name)
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] %#", ...)
So I'm new to iOS development and I'm trying to add a predicate to my NSFetchRequest() in order to filter out items located nearby the user's current location. However, it doesnt seem to be working and the NSFetchRequest returns empty.
let fetchRequest = NSFetchRequest()
fetchRequest.predicate = NSPredicate(format: "itemLatitude BETWEEN {%d, %d}", (latitude-10), (latitude+10))
If I remove the predicate the query returns all items so fetch does work, but not the predicate it seems. There's probably something really basic wrong here and I'd really appreciate any help.
try this :
let fetchRequest = NSFetchRequest()
fetchRequest.predicate = NSPredicate(format: "itemLatitude BETWEEN %#", [(latitude-10), (latitude+10)])
For more details on how use BETWEEN comparison see Apple documentation "Basic Comparisons" part.
Otherwise, you can code you predicate like this :
let fetchRequest = NSFetchRequest()
fetchRequest.predicate = NSPredicate(format: "itemLatitude>%f AND itemLatitude<%f", (latitude-10), (latitude+10))
The issue with your predicate seems to be in the string formating, as #vadian mentioned. Replacing %d to %f makes it work. However, I let you here a little tip for debugging this kind of Predicate issues in the future:
NSPredicate's description method will return you the string of the final Predicate being applied.
NSPredicate(format: "itemLatitude BETWEEN {%f, %f}", (latitude-10), (latitude+10)).description
NSPredicate(format: "itemLatitude BETWEEN {%f, %f}", (latitude-10), (latitude+10)).description
shows in a playground
itemLatitude BETWEEN {0, 0}
itemLatitude BETWEEN {13.45, 33.45}
#ben-messaoud-mahmoud's formating seems also appropriate for this predicate:
NSPredicate(format: "itemLatitude BETWEEN %#", [(latitude-10), (latitude+10)]).description
will also have as result:
itemLatitude BETWEEN {13.45, 33.45}
and is a more clear answer.
Cheers!
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 get the following problem:
'NSInvalidArgumentException', reason: 'Unable to generate SQL for predicate (category == Basis) (problem on RHS)'
case "Basisfragen":
request.predicate = NSPredicate(format: "category = Basis")
break
Does anybody have an idea why it doesn't work?
Wasn't able to find a helpful answer for Swift or to translate any Objective-C answer to a working one in Swift...
Thx!
Patrick
use this that assume that category is a key or attribute with string type.
request.predicate = NSPredicate(format: "category ==%#","Basis")