🍯 强大而优雅的 Swift 语法糖 🍯
享受 Swift 代码库的美妙语法增强
let fileManager = FileManager().this {
$0.urls(for: .applicationDirectory, in: .userDomainMask)
}
let tableView : UITableView = {
let table = UITableView()
table.backgroundColor = .white
table.register(UserCell.self, forCellReuseIdentifier: "CellID")
table.separatorStyle = .none
table.allowsSelection = false
return table
}()
let tableView = UITableView().this {
$0.backgroundColor = .white
$0.register(UserCell.self, forCellReuseIdentifier: "CellID")
$0.separatorStyle = .none
$0.allowsSelection = false
}
let point = CGPoint().set {
$0.x = 100
$0.y = 200
}
extension CustomType: Sukari {}
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
}
let emailTextField = UITextField().this {
$0.placeholder = "Email"
$0.borderStyle = .roundedRect
$0.font = UIFont.systemFont(ofSize: 14)
$0.backgroundColor = UIColor(white: 0, alpha: 0.03)
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(loginButton)
view.addSubview(emailTextField)
}
}
Unwrap 让你轻松地要求 Optional
值。
在任何你期望始终为非 nil 的 optional 值上使用 unwrap(),或者使用 .unwrap(debug:)
来崩溃你的 App 并提供更(可选的)描述性调试信息。
最重要的是,unwrap 还提供了一种语法增强,可以通过底层 Guard
语句轻松地 unwrap
optional 值。
在任何 Optional
上调用 unwrap()
,可以选择提供一个 debugMessage
用于调试目的
struct Person {
let name: String
let email: String
init(dictionary: [String: Any]) {
name = dictionary["name"].unwrap(debug: "Unable to find json Element Name") as! String
email = dictionary["email"].unwrap(debug: "Unable to find json Element Email") as! String
}
}
let dictionary = ["ame": "Chris", "email": "chrisbkarani@gmail.com"]
let chris = Person(dictionary: dictionary)
print(chris.name) //Chris
print(chris.email) // chrisbkarani@gmail.com
class LoginController: UIViewController {
var token: Token?
override func viewDidLoad() {
super.viewDidLoad()
// more code is more bugs
guard let unwrappedToken = token else {
// if this crashes we enter a 'nil' state in our app with no debug information
return
}
LoginService.login(unwrappedToken)
}
}
class LoginController: UIViewController {
var token: Token?
override func viewDidLoad() {
super.viewDidLoad()
LoginService.login(token.unwrap())
}
}
pod 'Sukari'
请阅读 CONTRIBUTING.md,了解我们的行为准则以及向我们提交 pull request 的流程。
此项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE.md 文件