Filter Array with dictionaries using NSPredicate
Please see the below example:
NSArray *array = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"filter string" forKey:@"email"]]; // you can also do same for Name key...
NSArray *filteredarray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(email == %@)", @"filter string"]];
NSPredicate to filter an Array of Dictionaries (Swift)
I agree with the comment by @luk2302, in that using the Swift Array
filter
method is nicer in general than filtering with an NSPredicate
. If you're worried about performance across a large data set you should have your content in CoreData
and use an NSPredicate
as part of an NSFetchRequest
with proper indexing setup in your data model.
Having said that, filtering by NSPredicate
is not explicitly supporting with the Swift Array
type, so you would have to cast to NSArray
to use it. It would look something like this:
let filteredArray = (arrayofDictionary as NSArray).filteredArrayUsingPredicate(speciesPredicate)
Filtering an array of arrays that contain a dictionary using NSPredicate
I am not sure if you can do it with a single predicate. But you can iterate through individual array of dictionaries, filter them and add the filtered list to your final array. Try something like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"desc contains[c] %@", searchText];
NSMutableArray *finalColletion = [NSMutableArray array];
for (NSArray *temp in self.charactersAndDescriptions) {
NSArray *result =[temp filteredArrayUsingPredicate:predicate];
if([result count] > 0) {
[finalColletion addObject:result];
}
}
Filter data from array of Dictionary using predicate iOS SDK
This will give you your desired output
Objective - C
NSArray *filteredData = [yourArrayContainingDictionary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", searchText]];
Swift
let filteredData = yourArrayContainingDictionary.filter{
let string = $0["mall_name"] as! String
return string.hasPrefix("searchText")
}
Hope this helps you :)
NSPredicate for Array of Dictionaries
You need the following
NSString *str = <search string>;
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY Mens.Name LIKE %@ OR ANY Womens.Name LIKE %@", str, str];
NSArray *result = [your_array filteredArrayUsingPredicate:pred];
BOOL success = result.count > 0;
Filtering with NSPredicate, for array count of Array inside dictionary inside array
In this case that results in -valueForKey: @"count"
being sent to each xyz instance, and xyz isn't key value coding compliant for count.
Instead, use the @count
operator to evaluate the count of the collection when you want the count then use
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.@count>2"];
instead of
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"xyz.count>2"];
NSPredicate on array of dictionaries iOS
try like this,
NSArray *filtered = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.id.%K == '1'",@"$id"]];
Related Topics
Stroke Width with a Scenekit Line Primitive Type
Nearby Bluetooth Devices Using Swift 3.0
Using Sysctlbyname() from Swift
Remote Image Size Without Downloading
iOS How to Dismiss Uialertview with One Tap Anywhere
Get User Swiping Direction in Uipageviewcontroller
How to Clear/Reset All Coredata in One-To-Many Relationship
iOS Share Gif (Animated Image) Not Working
iOS Apns "Best-Effort" Fallback
How to Do Authorization and Login with Wechat Using the iOS Sdk
iOS Permission Alerts - Removing or Suppressing
Ignore Manual Entries from Apple Health App as Data Source
Document Directory Path Change When Rebuild Application
Drawing with Cgpath to Svg Output
Wkwebview and Uimenucontroller
Uitableview Pagination - Bottom Refresh to Load New Data in Swift
Expandable Uitableview Cell Using Autolayout Results in Uiviewalertforunsatisfiableconstraints