Searching NSArray of NSDictionary objects
NSArray *contacts = ...; //your array of NSDictionary objects
NSPredicate *filter = [NSPredicate predicateWithFormat:@"contact_type = 42"];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:filter];
After this, filteredContacts
will contain only the contacts whose contact_type
is 42
.
If you need to search for more than one kind of contact_type
, then simply use an OR
in the predicate:
filter = [NSPredicate predicateWithFormat:@"contact_type = 42 OR contact_type = 23"];
Search NSArray of NSDictionary. How to find using NSPredicate?
You can use
NSArray *contacts = self.dataArray; //your array of NSDictionary objects
NSPredicate *filter = [NSPredicate predicateWithFormat:@"contact_detail.contact_Label = %@",stringValue];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:filter];
Happy coding...
Filtering NSArray of NSDictionary objects using NSPredicate
When using a dynamic key, you should use the %K
token instead of %@
. You also don't want the quotes around the value token. They will cause your predicate to test for equality against the literal string @"%@"
instead of against value
.
NSString *predicateString = [NSString stringWithFormat:@"%K == %@", key, value];
This is documented in the Predicate Format String Syntax guide.
Edit: As Anum Amin points out, +[NSString stringWithFormat:]
doesn't handle predicate formats. You want [NSPredicate predicateWithFormat:@"%K == %@", key, value]
instead.
Filtering NSArray of NSDictionary objects
for (int i = 0; i< responseArray.count; i++)
{
if (![cityArray containsObject:[[responseArray objectAtIndex:i] valueForKey:@"city"]])
{
[cityArray addObject:[[responseArray objectAtIndex:i] valueForKey:@"city"]];
}
}
load the first tableView with the cityArray
in didSelectRowAtIndex
:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"city = %@",[cityArray objectAtIndex:indexPath.row]];
NSArray *array = [responseArray filteredArrayUsingPredicate:predicate];
NSLog(@"Array -- %@",array);
Searching an NSArray full of dictionary objects not working
You will not get filtered data from the array instance that you have.. filteredArrayUsingPredicate will return the filtered array. change the code as follows..
- (void)filterArray: (NSString *)searchText
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(userSetname LIKE[cd] %@)", searchText];
self.originalResults = self.freindResults;
NSMutableArray *array = self.freindResults;
NSArray *filteredArry = [array filteredArrayUsingPredicate:predicate];
NSLog(@"Filtered array %@", filteredArry);
self.freindResults = filteredArry;
[self.tableView reloadData];
}
Filter NSArray with NSDictionary (nested level of object) using NSPredicate ANY and IN?
I think it's better to pass the array as a parameter with %@
placeholder.
[NSPredicate predicateWithFormat:@"ANY amenities.amenity_id IN %@", @[@"1",@"2",@"3"]]
That way, you can create manually your array, and if you need to change it, it's easier.
If you still want it to do it manually changing the "string format":
[NSPredicate predicateWithFormat:@"ANY amenities.amenity_id IN {'1', '2', '3'}"]
Searching for an object in an unknown NSDictionary/NSArray depth
Continuing from the answer, you need to do some modification:
- (id)findDictionaryWithValue:(id)value inArray:(NSArray *)array
{
__block id match = nil;
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSArray class])
{
match = [self findDictionaryWithValue:value inArray:obj];
}
else if ([obj isKindOfClass:[NSDictionary class])
{
match = [self findDictionaryWithValue:value inDictionary:obj];
}
*stop = (match!=nil);
}];
return match;
}
- (id)findDictionaryWithValue:(id)value inDictionary:(NSDictionary *)dict;
{
__block id match = nil;
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if([obj isEqual:value]) {
match = dict;
}
else if ([obj isKindOfClass:[NSArray class])
{
match = [self findDictionaryWithValue:value inArray:obj];
}
else if ([obj isKindOfClass:[NSDictionary class])
{
match = [self findDictionaryWithValue:value inDictionary:obj];
}
*stop = (match!=nil);
}];
return match;
}
NSDictionary *resultDict = [self findDictionaryForValue:@"i'm an id" inArray:array];
However, you must be careful with recursion. If there is any circular path, you will get an infinite loop( and stack overflow before crash).
Related Topics
Path Extension and Mime Type of File in Swift
Find Uialertview Without Having Reference to It iOS 7
Alamofire with a Self-Signed Certificate/Servertrustpolicy
Uiimagepicker Cameraoverlayview Appears on Retake Screen
Invalid Signature (Invalid Binary) on Itunes Connect
How to Only Disable Scroll in Scrollview But Not Content View
iOS Timed Background Processing
Pod Install Command with Error
iOS Web Page Errors Over Cellular Data But Not Over Wifi? Recent Change to At&T Cellular Network
How to Fade a Uivisualeffectview And/Or Uiblureffect in and Out
Accessing Variables from Another Viewcontroller in Swift
iOS 11 Layout Guidance About Safe Area for iPhone X
How to Remove Border from Segmented Control
Nsnotificationcenter Swift 3.0 on Keyboard Show and Hide
Swift: Random Number for 64-Bit Integers
Error in Xcode 6 - View Controller Does Not Have an Outlet Named (Subview)
Ipa Generated by Swift Is So Big, About 5Mb
Memory Leaks in the Swift Playground/Deinit{} Not Called Consistently