๐ Powerful, Clean, Elegant Initialisation Sugar ๐
This is a small syntactic extension to NSObject that makes it easier for you to initilialize your object in a very Swifty way.
-
this()
is an extension on all NSObject subclasseslet fileManager = FileManager().this { $0.urls(for: .applicationDirectory, in: .userDomainMask) }
Clean up your initialization Code
let tableView = UITableView().this {
$0.backgroundColor = .white
$0.register(UserCell.self, forCellReuseIdentifier: "CellID")
$0.separatorStyle = .none
$0.allowsSelection = false
}
Initialize in this way.
let tableView : UITableView = {
let table = UITableView()
table.backgroundColor = .white
table.register(UserCell.self, forCellReuseIdentifier: "CellID")
table.separatorStyle = .none
table.allowsSelection = false
return table
}()
-
Use your own Types With a simple Extension
extension CustomType: This {} let instance = CustomType().this { $0.color = .blue $0.label.text = "Custom Type" }
class LoginViewController : UIViewController {
var loginButton = UIButton().this {
$0.setTitle("Login", for: .normal)
$0.backgroundColor = . yellow
$0.layer.masksToBounds = true
$0.layer.cornerRadius = 5
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(loginButton)
}
}
-
For iOS 8+ projects with CocoaPods:
pod 'This'
just clone
the repo and run on Xcode
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
- Chris Karani -
This project is licensed under the MIT License - see the LICENSE.md file for details