Skip to content

Instantly share code, notes, and snippets.

@tonnylitao
Created November 17, 2016 07:23
Show Gist options
  • Save tonnylitao/90ca66335befccfa2e63e2035418cc9d to your computer and use it in GitHub Desktop.
Save tonnylitao/90ca66335befccfa2e63e2035418cc9d to your computer and use it in GitHub Desktop.
Using Protocol Extension Constraint
import Foundation
import UIKit
protocol Dao {}
extension NSObject: Dao {}
extension Dao where Self : NSObject {
typealias ConfigClosure = (Self) -> Void
init(_ closures: ConfigClosure...) {
self.init()
closures.forEach {
$0(self)
}
}
}
let btn = UIButton() {
$0.frame = CGRect(x: 10, y: 10, width: 44, height: 44)
$0.backgroundColor = .red
}
print(btn)
// or
let simplifyBtn = UIButton {
$0.frame = CGRect(x: 10, y: 10, width: 44, height: 44)
$0.backgroundColor = .red
}
print(simplifyBtn)
let dic = NSMutableDictionary {
$0["key"] = 10
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment