APGenericSearchTextField
Context
Swift 4 introduced a new type called KeyPath. It allows to access the properties of an object.
For instance:
let helloWorld = "HelloWorld"
let keyPathCount = \String.count
let count = helloWorld[keyPath: keyPathCount]
//count == 10
The syntax can be very concise, it supports type inference and property chaining.
Purpose
Long time ago, I had the necessity to create a SearchTextField, and I thought it would be nice to apply this concept to it.
Under The Hood
- KeyPath
 - NSPredicate
 
Example
To run the example project, clone the repo, and run pod install from the Example directory first.
Details
You have to follow few simple steps:
- Define your Model by subclassing NSObject
 - Define property by using @objc
 - Select filterOperator
 - Select propertyToFilter
 - Define your cellConfigurator
 
Model
class Person: NSObject {
	@objc let name: String
	@objc let surname: String
	init(name: String, surname: String) {
		self.name = name
		self.surname = surname
	}
}Filter Operator
At the moment only two operators are supported
- Contains
 - Equal
 
Property To Filter
Using keyPath you can choose what field you want to compare.
For istance: \.name if you want to filter your array objects by name
Cell Configurator
You can configure your cell in this way
searchTextField.cellConfigurator = { [weak self] (person, cell) in
			cell.textLabel?.text = person.name
}Customization
It's possible to customize your suggestion TableView with these values
tableXOffset
tableYOffset
tableCornerRadius
tableBottomMargin
maxResultsListHeight
minCharactersNumberToStartFilteringsingleItemHandler will return the selected object in tableView
singleItemHandler = { [weak self] value in
		print(value)
}Storyboard
Storyboards have a problem with having a generic class. The thing is that Interface Builder communicates to the ViewController through the Objective-C runtime. Because of this, InterfaceBuilder is limited to the features that Objective-C provides. In this case, generics are not supported. By the way there is a workaround to use generic class with storyboard.
You need to:
- 
Declare a custom
UITextFieldthat extendsGenericSearchTextFieldand add it to storyboardi.e. class SearchTextField: GenericSearchTextField<Person>{} - 
Define Outlet
- Defining an outlet of type 
SearchTextField, XCode will report an error. It's important to change type fromSearchTextFieldtoUIView - Before: 
@IBOutlet weak var textField: SearchTextField! - After: 
@IBOutlet weak var textField: UIView! 
 - Defining an outlet of type 
 - 
Define a computed property of type
GenericSearchTextField- Cast your variable to 
GenericSearchTextField - Now your are able to use your variable
 
 - Cast your variable to 
 
var searchTextField: GenericSearchTextField<Person> {
		return textField as! GenericSearchTextField
}- Use 
searchTextFieldvariable 
Bugs
The library is in alpha stage
Found a bug? Simple Pull Request
TODOs
- Better test coverage
 - Better Filter management
 - Custom cell support
 Storyboard Support
Demo
Check out the Example project. You will find two examples with programmatically and storyboard.
Requirements
Installation
APGenericSearchTextField is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'APGenericSearchTextField'Author
Bellaposa, [email protected]
License
APGenericSearchTextField is available under the MIT license. See the LICENSE file for more info.
