Created
November 17, 2016 07:23
-
-
Save tonnylitao/90ca66335befccfa2e63e2035418cc9d to your computer and use it in GitHub Desktop.
Using Protocol Extension Constraint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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