Explore some cool generic techniques which you could use for building complex ui in a declarative manner.
TableKit is a super lightweight yet powerful generic library that allows you to build complex table views in a declarative type-safe manner. It hides a complexity of UITableViewDataSource and UITableViewDelegate methods behind the scene, so your code will be look clean, easy to read and nice to maintain.
https://github.com/maxsokolov/TableKit
6. Generic code enables you to write flexible, reusable functions
and types that can work with any type, subject to requirements
that you define. You can write code that avoids duplication and
expresses its intent in a clear, abstracted manner.
Apple docs
What for?
6
16. What run time means?
DataProvider *provider = [DataProvider new];
[provider fetch:@"/users"
mappingClass:[NSString class] // compiler doesn’t warn you here...
completion:^(NSArray<User *> *results) {
}];
16
17. You are doing it wrong with Swift
let dataProvider = DataProvider()
dataProvider.fetch("/resource", mappingClass: User.self) { result in
// code smell...
guard let users = result.values as? [User] else {
return
}
}
17
24. Problems with UITableView
• Boilerplate
• Painful when data sources are complex and dynamic
• Not type safe (dequeueReusableCellWithIdentifier etc.)
24
25. What table view actually needs?
Model ViewModel
UITableViewCell
Deadline ;)
25
33. The power of declarative approach
let rows: [Row] = strings.map { TableRow<String, MyTableViewCell>(item: $0) }
34. The view layer is simply
a mapping of our data layer
let rows: [Row] = users
.filter({ $0.active })
.map({ UserViewModel(user: $0) })
.map({ TableRow<UserViewModel, UserTableViewCell>(item: $0) })
35. Resume
• Currently, SDK/API’s are not generic
• Compiler is not perfect
• Can’t be used with Objective-C
• Compile-time type-safety help to know about potential
issues earlier
• We could build great, flexible, easy to use API’s
35