Apply NSPredicate on [(String, ArrayString)]
Since your array contains tuples, instead of using NSPredicate
, you can use the following method for filtering your data:
func getFilteredData(data : [(String, Array<String>)], ltrToCompare : String) -> [(String, Array<String>)]
{
// For keeping the filtered result
var returnData = [(String, Array<String>)]()
// Looping through parent array
for (letter, arr) in data
{
// Filters the internal array [String]
let filter = arr.filter()
{
return $0.lowercaseString.rangeOfString(ltrToCompare.lowercaseString) != nil
}
// Checks whether the inner array filtering returns any element
if (filter.count != 0)
{
returnData.append((letter, filter));
}
}
return returnData
}
Call that method from:
func updateSearchResultsForSearchController(searchController: UISearchController)
{
data = getFilteredData(data, ltrToCompare: searchController.searchBar.text)
//reload tableView with fresh data
self.tableView.reloadData()
}
Search array of array of strings with NSPredicate
You need to use ANY SELF
.
let pairs = [["key1","value1"],["key2","value2"]]
let searchPredicate = NSPredicate(format: "ANY SELF CONTAINS[c] %@", "lue2")
let searcher = (pairs as NSArray).filteredArrayUsingPredicate(searchPredicate)
println(searcher) // Prints the second element
From the official documentation for ANY
:
Specifies any of the elements in the following expression.
iOS: How to use NSPredicate to filter string *Array
First of all, You need to convert an array of NSString
to an array of NSDate
to apply filter with NSDate
. Please use the below code to achieve your requirement.
Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM"];
NSMutableArray *muarray = @[
@"2019/6",@"2019/5",@"2019/4",@"2019/3",@"2019/2",@"2019/1",@"2018/12",@"2018/11",@"2018/10",@"2018/9",@"2018/8",@"2018/7",@"2018/6",@"2018/5",@"2018/4",@"2018/3",@"2018/2",@"2018/1",@"2017/12",@"2017/11",@"2017/10",@"2017/9",@"2017/8",@"2017/7",@"2017/6",@"2017/5",@"2017/4",@"2017/3",@"2017/2",@"2017/1"
].mutableCopy;
NSMutableArray* aryDates = [NSMutableArray new];
for (NSString* strDate in muarray) {
NSDate *dateFromString = [dateFormatter dateFromString:strDate];
[aryDates addObject:dateFromString];
}
NSDate* date1 = [dateFormatter dateFromString:@"2017/8"];
NSDate* date2 = [dateFormatter dateFromString:@"2018/8"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN %@", [NSArray arrayWithObjects:date1, date2, nil]];
[aryDates filterUsingPredicate:predicate];
NSLog(@"%@",aryDates);
Predicate to filter array of strings in SWIFT throws error saying NSCFString is not key-value coding
NSPredicate(format:)
strongly expects to be used with printf-style format strings (it automatically quotes arguments as it inserts them, etc).
You're using Swift's string interpolation, so the already-formatted query comes to NSPredicate
as a single string. That keeps it from doing whatever voodoo it does with arguments, leaving you with a malformed query.
Use printf-style formatting instead:
if let predicate = NSPredicate(format: "SELF CONTAINS %@", searchText) {
self.listItemToBeDisplayed = (listItem as NSArray).filteredArrayUsingPredicate(predicate)
(self.view.viewWithTag(1) as UITableView).reloadData()
}
Using NSPredicate to search an array with an array
Considering array
contains card Object.
NSArray *keyWordsList = [keywords componentSeparatedByString:@","];
[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ANY %K IN %@",@"tags",keyWordsList]]
EDIT:
To search partially you can use LIKE operator.
[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ANY %K LIKE[cd] %@",@"tags",[partialkeyWord stringByAppendingString:@"*"]]]
How to use NSPredicate to filter an array stored in CoreData?
You cannot do that. Check this answer core data array of string filter
You might consider to have an entity for tags, or join the array of tags and have it as a whole string in your current entity, thus having the tags property as type String. For the last approach, you could search with the predicate below.
NSPredicate(format: "tags CONTAINS[cd] %@", searchText)
The [cd] expression is to specify case and diacritic insensitivity respectively.
You can get know more about the Core Data operations here.
I got some basic operators from the documentation and put them below:
BEGINSWITH
The left-hand expression begins with the right-hand expression.
CONTAINS
The left-hand expression contains the right-hand expression.
ENDSWITH
The left-hand expression ends with the right-hand expression.
LIKE
The left hand expression equals the right-hand expression: ? and * are allowed as wildcard characters, where ? matches 1 character and * matches 0 or more characters.
Related Topics
Sync Data Between Two Viewcontrollers to Avoid Creating Same Observer Again
Uitableview with Uiviewrepresentable in Swiftui
How to Use Alamofires Servertrustpolicy.Disableevaluation in Swift 3
Uiimageview .Scaleaspectfit and Autolayout Not Working Programmatically from Swift
Delegate Seems to Not Be Working, According to the Console
Calling Delegate Function from Appdelegate Not Working
Get Total Include Months Between 2 Date in Swift
Extract 4 Bits of Bluetooth Hex Data
How to Crop Image in Circle Shape with Grid Inside Circle
How to Parse a Iso 8601 Duration Format in Swift
Swift - How to Open Another Viewcontroller with Collectionviewcell Inside Uitableviewcell
Swift Computed Properties Cannot Be Used in Init
Deinit Method Is Not Called in Xcode 10 Beta 6 Playground
Making the Map Zoom to User Location and Annotation (Swift 2)
How to Change Uinavigationbar Per View Controller
Center Horizontal Uicollectionview Based on Content
Add View as a Parameter to a Custom Viewmodifier
iOS PDFkit Displaymode = Singlepage Only Shows the First Page of the PDF