Sukari

🍯 强大而优雅的 Swift 语法糖 🍯

描述

享受 Swift 代码库的美妙语法增强

只需将 Sukari 添加到你的初始化器中

使用 .this{} 快速初始化! 🌈

    
    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,为你的类型添加糖分 🔌

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(解包)

Unwrap 让你轻松地要求 Optional 值。

在任何你期望始终为非 nil 的 optional 值上使用 unwrap(),或者使用 .unwrap(debug:) 来崩溃你的 App 并提供更(可选的)描述性调试信息。

最重要的是,unwrap 还提供了一种语法增强,可以通过底层 Guard 语句轻松地 unwrap optional 值。

  1. 不再有 if let 金字塔和 Guard let 高塔。 只有对你的 optionals 的一个干净而简单的扩展 unwrap
  2. 通过 unwrap(debug:) 接收丰富的调试信息 screen shot 2017-12-19 at 17 35 03

立即将这个简单但有效的增强功能应用于您的代码库!

发现 Guard let 高塔。

screen shot 2017-12-20 at 12 57 13

干净、简洁,并提供更具描述性的崩溃信息!

screen shot 2017-12-20 at 13 03 52

用法

在任何 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

另一个真实世界的例子

不使用 Unwrap

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)
    }
    
}

使用 Unwrap

class LoginController: UIViewController {
    var token: Token?
    override func viewDidLoad() {
        super.viewDidLoad()
        LoginService.login(token.unwrap())
    }
}

安装

贡献

请阅读 CONTRIBUTING.md,了解我们的行为准则以及向我们提交 pull request 的流程。

作者

Chris Karani

许可

此项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE.md 文件