I'm trying to get all the values of keys that are only in an array after filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
if ([scope isEqualToString:#"Calendar"]) {
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#", searchText];
self.searchResults = [NSMutableArray arrayWithArray:[[allsearchDictKeys filteredArrayUsingPredicate:resultPredicate] sortedArrayUsingSelector:#selector(localizedStandardCompare:)]];
}
}
How can I get an array, that has the values for the keys in self.searchResults array only, with the same indexPaths as their counterpart?
This is being passed into my UISearchResults updating tableView, so I would like this to occur prior to the update so I don't have to do much in cellForRowAtIndexPath. I know I can enumerate through them, and add those objects to another array, but my question is can I bypass that and just simply directly add them to an array?
Example:
self.searchResultsValuesArray = [searchDict valuesForKeys:self.searchResults];
I know there is no valuesForKeys but it's there for illustrative purposes.
You can use the NSDictionaryMethod objectsForKeys:notFoundMarker:
self.searchResultsValuesArray = [searchDict objectsForKeys:self.searchResults notFoundMarker:#""];
Related
It is possible to filter an array of strings as follows:
NSArray *array = #[#"honda",#"toyota",#"ford"];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF contains[cd] %#",#"ford"];
NSArray *filtered = [array filteredArrayUsingPredicate:pred];
I want to search an array that contains arrays of two strings by the values for the first of the strings. So for:
NSArray *cars = #[#[#"honda",#"accord"],#[#"toyota",#"corolla"],#[#"ford",#"explorer"]];
I want to search the first dimension (honda, toyota, ford) for #"ford"
Is there a way to tell the predicate I want to search on only the first attribute and return matching elements of the array?
Well here is the pred you need.
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF[FIRST] contains[cd] %#", #"ford"];
In the following reversedArray has three or more strings such as Salads, Meats Appetizer in order.
However, I want to have Meats always to be the first string in the array.
NSPredicate *predicateMain = [NSPredicate predicateWithFormat:
#"(%K == %#)", #"categoryType", #"main"];
NSPredicate *predicateSide = [NSPredicate predicateWithFormat:
#"(%K == %#)", #"categoryType", #"side"];
NSPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:
[NSArray arrayWithObjects:predicateMain, predicateSide,nil]];
NSArray *filteredArray = [foods filteredArrayUsingPredicate:orPredicate];
NSArray *reversedArray = [[[filteredArray valueForKeyPath:
#"#distinctUnionOfObjects.categoryName"]
reverseObjectEnumerator] allObjects];
I can do it via hardcode but I want to know proper way of handling.
To avoid hardcoding you could write a function to reorder the array with any given string as the first string, and utilize that.
For example:
void yourFunctionName(string firstString, NSArray &array){
//Iterate through array
//Check if you've found a string matching firstString
//Put it at the front by moving everything else down one
//Continue to iterate until you've reached the end of the array
}
Here it would be best to pass the array by reference (using the &) so that you modify the array itself and not just a copy of the array values (what you get when you don't pass by reference).
I'm trying to use a predicate to filter objects where an intersection exists between two arrays.
The NSManagedObject has an array(Of Strings) attribute named "transmissions". And there is another array(Of Strings) that will contain words to filter by, named "filters".
I'm not sure how to find if any elements in "transmissions" match any element in "filters".
I've tried
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY SELF.transmission in[c] %#",transmissions];
or
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY transmission in[c] %#",transmissions];
However, core data fetches no results where there should be some.
Try this.
NSPredicate *predicate = nil;
NSMutableArray *predicates = [NSMutableArray new];
for (NSString *transmission in transmissions) {
[predicates addObject:[NSPredicate predicateWithFormat:#"transmission == %#", transmission]];
}
if (predicates.count > 0)
predicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];
// make fetch request using 'predicate'
You can use the keyword CONTAINS to check an object exists in a collection.
[NSPredicate predicateWithFormat:#"favouriteFilms CONTAINS %#", film]
I believe the same thing can be achieved with IN by switching the LHS and RHS of the predicate. I have only successfully implemented this functionality using CONTAINS however.
I want to use NSPredicate to search for a similar/matching string in an array of an array and return the array that contains the string.
Below is an example of the 2D array that I am wanting searched.
NSMutableArray *people [
[#"Jane", #"Doe"],
[#"John", #"Smith"]
]
Example: Searching "Jane" will return the array [#"Jane", #"Doe"]
Note: The inner arrays are NSMutableArray's, NOT NSArray's.
I tried using this code:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#", searchText];
searchResults = [people filteredArrayUsingPredicate:resultPredicate];
but no results are shown no matter what I type and an alert that tells me the count of searchResults reports 0.
I'm using UISearchBar in my app.
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filtered removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.companyName contains[cd] %#",searchText];
NSArray *tempArray = [ee filteredArrayUsingPredicate:predicate];
filtered = [NSMutableArray arrayWithArray:tempArray];
}
I did NSLog on above "filtered" array, it's getting proper search results.
I don't see the filtered values in the tableview. I see "No results" if I type wrong search text. But for the correct search text, the table view is plain empty.
Can anyone help me what needs to be done?
Thanks in advance!!
Please use the following code . And make sure that self.filtered is the datasource of yourtableview . Datasource means array
from tableview gets populated
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filtered removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.companyName contains[cd] %#",searchText];
NSArray *tempArray = [ee filteredArrayUsingPredicate:predicate];
filtered = [NSMutableArray arrayWithArray:tempArray];
[yourtableview reloadData];
}
Write the above code then Reload the table in the source code
[tableView reloadData];