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.
Long time ago, I had the necessity to create a SearchTextField, and I thought it would be nice to apply this concept to it.
- KeyPath
- NSPredicate
To run the example project, clone the repo, and run pod install
from the Example directory first.
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
class Person: NSObject {
@objc let name: String
@objc let surname: String
init(name: String, surname: String) {
self.name = name
self.surname = surname
}
}
At the moment only two operators are supported
- Contains
- Equal
Using keyPath you can choose what field you want to compare.
For istance: \.name
if you want to filter your array objects by name
You can configure your cell in this way
searchTextField.cellConfigurator = { [weak self] (person, cell) in
cell.textLabel?.text = person.name
}
It's possible to customize your suggestion TableView with these values
tableXOffset
tableYOffset
tableCornerRadius
tableBottomMargin
maxResultsListHeight
minCharactersNumberToStartFiltering
singleItemHandler will return the selected object in tableView
singleItemHandler = { [weak self] value in
print(value)
}
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
UITextField
that extendsGenericSearchTextField
and 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 fromSearchTextField
toUIView
- 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
searchTextField
variable
The library is in alpha stage
Found a bug? Simple Pull Request
- Better test coverage
- Better Filter management
- Custom cell support
Storyboard SupportSwift Package Manager
Check out the Example project. You will find two examples with programmatically and storyboard.
APGenericSearchTextField is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'APGenericSearchTextField'
Bellaposa, antonioposabella91@gmail.com
APGenericSearchTextField is available under the MIT license. See the LICENSE file for more info.