Skip to content

yingmu52/Snakepit

Repository files navigation

Build Status ios Swift 4.1 CocoaPods compatible License MIT PRs Welcome

Installation

pod 'Snakepit'

Examples

Use AlertController with chained promise style

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

  }

Typesafe Storyboard

Let's say you have a UITabBarController in Main.storyboard called TabBarViewController, make sure its storyboard ID is also TabBarViewController

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

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

// Step 2
let tabVc = Storyboard.Main.get(TabbarViewController.self)

Typesafe TableViewCell

Let's say you have a custom prototype cell MyCell, make sure its reuse identifier is MyCell.

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

you can use tableView.register(cell: MyCell.self) in viewDidLoad if you want to register a cell from a .xib

Hex UIColor

UIColor(0xFF0000) // the same as UIColor.red
UIColor(0x00FF00) // the same as UIColor.green
UIColor(0x0000FF) // the same as UIColor.blue

Target-Action using closure

UIButton().onTouch(for: .touchUpInside) {
  print("button pressed")
}

UISwitch().onTouch(for: .valueChanged) {
  print("switch toggled")
}

UITapGestureRecognizer().onTouch {
  print("tap detected")
}


UIBarButtonItem(title: "item", style: .plain) {
  print("item pressed")
}

Typesafe UserDefault

Let's say you want to store a URL in UserDefault with key myURL and a phone number (String) with key 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()