NSPredicate IN query from array elements
You would construct a predicate like this:
NSPredicate(format:"connectionType IN %@", yourIntArray)
I hope this is helpful.
How to create NSPredicate with array contains element
You cannot search in an array defined as transformable attribute with a predicate. Better define another entity
@objc(Favourite)
class Favourite: NSManagedObject {
@NSManaged var name: String
}
and a one-to-many relationship from "Address" to "Favourite".
The you can fetch all addresses which have any (i.e.: at least one) favorite equal to the string "a" with the predicate
NSPredicate(format: "ANY favourites.name == %@", "a")
or better with a compiler-checked key path literal:
NSPredicate(format: "ANY %K == %@", #keyPath(Address.favourites.name), "a")
For a case-insensitive comparison you can use
NSPredicate(format: "ANY %K ==[c] %@", #keyPath(Address.favourites.name), "a")
Using NSPredicate with array for cloudKit search
Try replacing:
"Category == %@"
With:
"Category IN %@"
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;
}
)
NSPredicate: search for an array of values in relation, NOT
This can be achieved with a “SUBQUERY”:
let fansPredicate = NSPredicate(format: "SUBQUERY(fans, $f, $f.personid IN %@).@count = 0",
visitors)
The predicate is true for all Rockstar objects which have no related Person object whose personId
is in the given list.
Using NSPredicate to search through array of objects
First of all, if you substitute a keypath you should use %K
as arg.
Further, I think you are missing the ANY
argument in your second query. I think you want a result if any of the tag names contains your textSearch
.
To get a better understanding of how predicates work, have a look at the Apple Documentation
I did a quick test and it is still working fine:
NSMutableArray *arrayContacts = [NSMutableArray array];
{
AMContact *contact = [[AMContact alloc] init];
NSMutableArray *arrayTags = [NSMutableArray array];
{
AMTags *tag = [[AMTags alloc] init];
tag.name = @"Test";
[arrayTags addObject:tag];
}
{
AMTags *tag = [[AMTags alloc] init];
tag.name = @"Te2st2";
[arrayTags addObject:tag];
}
{
AMTags *tag = [[AMTags alloc] init];
tag.name = @"No";
[arrayTags addObject:tag];
}
contact.tags = [arrayTags copy];
[arrayContacts addObject:contact];
}
{
AMContact *contact = [[AMContact alloc] init];
NSMutableArray *arrayTags = [NSMutableArray array];
{
AMTags *tag = [[AMTags alloc] init];
tag.name = @"Test";
[arrayTags addObject:tag];
}
contact.tags = [arrayTags copy];
[arrayContacts addObject:contact];
}
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY SELF.%K.name contains[c] %@", @"tags", @"Test"];
NSArray *result = [arrayContacts filteredArrayUsingPredicate:pred];
NSLog(@"%@", result);
NSPredicate on array of arrays where some empty
If you’re interested in a stronger-typed filtering operation, and you’re using the latest Xcode 6.3, you can use flatMap
, which will perform a mapping operation that returns arrays on an array of arrays, and flattens the result. Then you can use filter in the subarrays:
// assuming birds is an NSArray containing NSArrays of Bird
if let arrayOfArrays = birds as? [[Bird]] {
let filteredBirds = arrayOfArrays.flatMap { birds in
birds.filter { bird in
bird.common_name.rangeOfString(searchText) != nil
}
}
}
Related Topics
Swift Sprite Kit in App Purchase
Convert String to Staticstring
How to Select All Text in a Uitextfield Using Swift
Nsinvocationoperation' Is Unavailable in Xcode 6.1
How to Print Escape Sequence Characters in Swift
Generic and (Early) Binding in Swift 1.2
How to Stop/Cancel Playsoundfilenamed in Swift
Convert Half Precision Float (Bytes) to Float in Swift
Use of Nspathcontrol to Represent Virtual Path
Best Way to Structure My Firebase Database
Nspredicate in Query from Array Elements
How to Convert 4 Bytes to a Swift Float
How to Cast Up to Super Class When There Is an Override Function in the Sub Class
Swift Combine: What Are Those Multicast Functions for and How to Use Them