The below predicate is worked for me.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"mcu.family LIKE \"STM32\" AND mcu.serie IN {\"STM32F3\"}"];
How to assign the mcu relationship as common to whole attribute.As like as below,
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"((family LIKE \"STM32\") AND (serie IN {\"STM32F3\"})) _ mcu"];
What can I assign the key in a underscore(_) Place?
Kindly help me.
Related
I have linking object in my category.
#property (readonly) RLMLinkingObjects *relatedAttachments;
I only want to take category if it have relatedAttachments. I try with this.
NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"relatedAttachments.count > 0"]];
self.categories = [[Category objectsWithPredicate:pred] sortedResultsUsingProperty:#"id" ascending:YES];
I got error like this.
RLMPrecondition(property, #"Invalid property name",
So I try with other.
NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"relatedAttachments.count!=nil"]];
NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:#"relatedAttachments.count!=NULL"]];
But it is still not okay. How shall I do?
You're looking for a predicate like:
[NSPredicate predicateWithFormat:#"relatedAttachments.#count > 0"];
#count is a collection operator that evaluates to the number of elements in the collection it appears to the right of.
I need to filter a list of contacts, but not copy that after a certain amount of letters attached filter is damaged sample images and the NSPredicate I'm using.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"CAST(SELF.compositeName, 'NSString') contains[cd] %#",self.SearchNames.text];
Your Predicate syntax is wrong.
let predicate = NSPredicate(format: "SELF.compositeName CONTAINS[cd] %#", searchText)
let filtredArray = NSArray(array: YourContactarray.filteredArrayUsingPredicate(predicate))
When we work with array of dictionary then simply SELF.key is valid for predicate.
After this code reload your table with filtredArray which you can stored in another array for global access. Let me know if you have any problem in this code.
First thing, you have to filter the array using NSPredicate. You can follow the below link for filtering the array.
NSPredicate Example
In that link, there are many examples for filtering array using using NSPredicate.
Here array means, which is used for displaying in UITableView.
Still you have questions, please post the code with array what you have used.
Try this
NSPredicate *predicate = [NSPredicate predicateWithFormat: #"compositeName CONTAINS[c] %#", searchText];
I am trying to create a NSPredicate with multiple conditions. I've found several solutions, but none of them appear to be working with my method. The best looking one I've found is below.
This is my single predicate method, and it works just fine:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"name contains[c] %#",
searchText];
filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];
Here is my edited version with multiple conditions. I'm not sure whats going wrong. Any ideas?
NSPredicate *p1 = [NSPredicate predicateWithFormat:#"name contains[c] %#", searchText];
NSPredicate *p2 = [NSPredicate predicateWithFormat:#"businessArea contains[c] %#",
searchText];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:#[p1, p2]];
filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];
You can try this
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:#[p1, p2]];
Addition to #Nikunj's answer, you can also use NSCompoundPredicate for your AND operations like this.
Obj-C - AND
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:#"X == 1"];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:#"X == 2"];
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:#[predicate1, predicate2]];
Swift - AND
let predicate1:NSPredicate = NSPredicate(format: "X == 1")
let predicate2:NSPredicate = NSPredicate(format: "Y == 2")
let predicate:NSPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate1,predicate2] )
Swift 3 - AND
let predicate1 = NSPredicate(format: "X == 1")
let predicate2 = NSPredicate(format: "Y == 2")
let predicateCompound = NSCompoundPredicate.init(type: .and, subpredicates: [predicate1,predicate2])
Nothing looks wrong in the code you posted, which means the error is probably coming from when you evaluate the predicate by filtering the array.
Since the first predicate works, the problem lies with the businessArea key path.
Filtering the array would throw an exception if:
There's an object in the array that doesn't have a businessArea value (as in, it's not an object that has a -businessArea method)
The object does have a businessArea value, but the value is neither an NSString nor nil
NSCompoundPredicate received a facelift. Just in case someone's looking for a Swift 5+ version:
AND
let predicatesAND = NSCompoundPredicate(andPredicateWithSubpredicates:[pred1,pred2])
OR
let predicatesOR = NSCompoundPredicate(orPredicateWithSubpredicates:[pred1,pred2])
First off, I suspect you may have a flaw in your two basic predicates. Judging by your object name "businesses", it is a collection i.e NSArray, NSSet or NSDictionay of items. In that case, "name contains[c] 'mmm'" has no meaning when evaluated for the whole collection, and should crash. It is missing a collection operator (ANY, ALL, SOME) , e.g. "ANY name contains[c] 'mmm'".
Ah... now I see... you're using the predicate to filter an NSArray, which means the predicate is executed on each item separately. So your predicates are alright.
Now... the simplest way to combine your predicates is at the very format-string level! what's wrong with
NSPredicate *p1 = [NSPredicate predicateWithFormat:#"name contains[c] %# AND businessArea contains[c] %#", nameToSearch, areaToSearch];
If you insist on doing it the hard way, than building your compound NSPredicate should have worked too - only you employed the same searchText to both conditions, which may caused the predicate to never match.
My advice to you, place a breakpoint on the line evaluating the filtered array and stop just before the filtering, and in the debugger print the predicate description, using the debugger command po p1 you will see there what is the final form of your predicate, and will be able to determine if you built it correctly.
I am new to NSPredicates, but I know the basics.
This is how my predicate looks now:
[NSPredicate predicateWithFormat:#"name contains[c] %#", searchText];
What I want to do, is to be able to create a predicate like this (Pseudo code):
NSPredicate *myPredicate = [NSPredicate predicateWithBaseString:#"name contains[c] _ARGUMENT_"];
And then use it in, for example a loop (pseudo code):
for(NSString *searchString in self.allStrings)
{
NSPredicate *myNewPredicate = [myPredicate predicateWithArgument:searchString];
//Here I do the searchOperations with my new predicate
}
I want to do this so that I can have a few base predicates and then put parameters on them for searching. I do not want to create my predicate again.
Hopefully you understand.
Thank you!
Use following
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:#"name contains[c] $ARGUMENT"];
for(NSString *searchString in self.allStrings)
{
NSPredicate *myNewPredicate = [myPredicate predicateWithSubstitutionVariables:#{#"ARGUMENT" :searchString}];
//Here I do the searchOperations with my new predicate
}
also I suggest to read this article http://nshipster.com/nspredicate/
It seems as if i just found the answer. I can use predicateWithSubstitutionVariables like this:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"name contains[c] $searchText"];
NSDictionary *sub = [NSDictionary dictionaryWithObject:searchText forKey:#"searchText"];
NSPredicate *filter = [resultPredicate predicateWithSubstitutionVariables:sub];
I wrote the following predicate for the following structure, not sure if its working correctly:
I have a Car.h managedobject
Car.h - name,
make,
model.
toCarParts relationship - CarPart.h - wheel,
tire,
mirror,
etc...
i have an array with many Car.h objects
I want to find only those that have a tire (so i have to look through toCarParts at all objects, and if any of the CarParts match my query, I need to pull that car into a results array)
will this predicate do this?
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY toCarParts.name == [c] %#", carPartString];
Thank you in advance
I believe this should work for you.
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"ANY toCarParts.name like %#", carPartString];