Snakepit 0.0.14

Snakepit 0.0.14

庄新意维护。



Snakepit 0.0.14

  • 作者
  • 庄新意

Build Status ios Swift 4.0 CocoaPods compatible License MIT PRs Welcome

安装

pod 'Snakepit'

示例

  • 使用 AlertController 与链式 Promise 风格
  override func viewDidLoad() {
    super.viewDidLoad()

    showAlert("This is an alert")
      .cancelAction(title: "Cancel") { print("Cancel pressed") }
      .action(title: "Confirm") { print("Confirm pressed") }

    showActionSheet("This is an action sheet")
      .action(title: "option 1") { print("option1 pressed") }
      .action(title: "option 2") { print("option2 pressed") }
      .action(title: "option 3") { print("option3 pressed") }
      .cancelAction(title: "Cancel") { print("Cancel pressed") }

  }
  • 类型安全 Storyboard

假设您在 Main.storyboard 中有一个名为 TabBarViewControllerUITabBarController,请确保其 Storyboard ID 也为 TabBarViewController

// Step 1
enum Storyboard: String, StoryboardGettable { 
  case Main

  var bundle: Bundle? {
    return Bundle.main 
  }
}

// Step 2
let tabVc = Storyboard.Main.get(TabbarViewController.self)
  • 类型安全 TableViewCell

假设您有一个自定义的原型 Cell MyCell,请确保它的重用标识符为 MyCell

  override func tableView(_ tableView: UITableView,
                          cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.deque(cell: MyCell.self, for: indexPath)
    return myCell
  }

如果您想在 viewDidLoad 中注册一个从 .xib 的 Cell,可以使用 tableView.register(cell: MyCell.self)

  • 十六进制 UIColor
UIColor(0xFF0000) // the same as UIColor.red
UIColor(0x00FF00) // the same as UIColor.green
UIColor(0x0000FF) // the same as UIColor.blue
  • 使用闭包的 Target-Action
UIButton().onTouch(for: .touchUpInside) {
  print("button pressed")
}

UISwitch().onTouch(for: .valueChanged) {
  print("switch toggled")
}
  • 类型安全 UserDefault

假设您想用密钥 myURLUserDefault 中存储一个 URL,以及一个电话号码(String)和一个密钥 phone

// Define keys
enum UserDefaultsKey: String, UserDefaultsGettable {
  case myUrl
  case phone
  static var bundle: Bundle {
    return Bundle.main
  }
}

// Save to UserDefault
let url = URL(string: "www.google.com")
UserDefaultsKey.myUrl.set(url)

// Retrive from UserDefault
let myURL = UserDefaultsKey.myUrl.get(URL.self)

// Remove from UserDefault 
UserDefaultsKey.myUrl.remove()