Merge pull request #93 from JK0369/add-extension Add extension for JSONDecoder and JSONEncoder
Merge pull request #93 from JK0369/add-extension
Add extension for JSONDecoder and JSONEncoder
✨ Super sweet syntactic sugar for Swift initializers.
Initialize UILabel then set its properties.
let label = UILabel().then { $0.textAlignment = .center $0.textColor = .black $0.text = "Hello, World!" }
This is equivalent to:
let label: UILabel = { let label = UILabel() label.textAlignment = .center label.textColor = .black label.text = "Hello, World!" return label }()
You can use then() to all of NSObject subclasses.
then()
NSObject
let queue = OperationQueue().then { $0.maxConcurrentOperationCount = 1 }
Want to use with your own types? Just make extensions.
extension MyType: Then {} let instance = MyType().then { $0.really = "awesome!" }
Use with() when copying the value types.
with()
let newFrame = oldFrame.with { $0.size.width = 200 $0.size.height = 100 } newFrame.width // 200 newFrame.height // 100
Use do() to do something with less typing.
do()
UserDefaults.standard.do { $0.set("devxoul", forKey: "username") $0.set("devxoul@gmail.com", forKey: "email") $0.synchronize() }
Here’s an example usage in an UIViewController subclass.
final class MyViewController: UIViewController { let titleLabel = UILabel().then { $0.textColor = .black $0.textAlignment = .center } let tableView = UITableView().then { $0.backgroundColor = .clear $0.separatorStyle = .none $0.register(MyCell.self, forCellReuseIdentifier: "myCell") } override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(self.titleLabel) self.view.addSubview(self.tableView) } }
Using CocoaPods:
pod 'Then'
Using Swift Package Manager:
import PackageDescription let package = Package( name: "MyAwesomeApp", dependencies: [ .Package(url: "https://github.com/devxoul/Then", majorVersion: 2), ] )
Then is under MIT license. See the LICENSE file for more info.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
Then
✨ Super sweet syntactic sugar for Swift initializers.
At a Glance
Initialize UILabel then set its properties.
This is equivalent to:
Tips and Tricks
You can use
then()
to all ofNSObject
subclasses.Want to use with your own types? Just make extensions.
Use
with()
when copying the value types.Use
do()
to do something with less typing.Real World Example
Here’s an example usage in an UIViewController subclass.
Installation
Using CocoaPods:
Using Swift Package Manager:
License
Then is under MIT license. See the LICENSE file for more info.