Skip to content

Instantly share code, notes, and snippets.

View stremsdoerfer's full-sized avatar

Emilien Stremsdoerfer stremsdoerfer

  • Her
  • San Francisco
View GitHub Profile
class ChatViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.textField.text = UserDefaults.chatDraft(chatId).value
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UserDefaults.chatDraft(chatId).value = self.textField.text //Type safe, you can't set anything but a String
extension UserDefaults {
static var token = StandardPair<String>(key: "token")
static var pinnedConversationIds = StandardPair<[Int]>(key: "pinnedConversationIds")
static var chatDraft = {(chatId: Int) in StandardPair<String>(key: "chatDraft\(chatId)")}
}
struct StandardPair<T> {
let key: String
var value: T? {
set {
if let safeValue = newValue {
UserDefaults.standard.set(safeValue, forKey: key)
} else {
UserDefaults.standard.removeObject(forKey: key)
}
class ViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
searchBar.rx.text.throttle(0.2, scheduler: MainScheduler.instance).subscribe(onNext: {(searchText) in
self.label.text = "new value: \(searchText)"
}).addDisposableTo(bag)
}
Alamofire.request("https://yourapi.com/login", method: .post, parameters: ["email":"test@gmail.com","password":"1234"]).responseJSON { (response:DataResponse<Any>) in
if response.response?.statusCode == 200 {
self.navigationController?.pushViewController(NewViewController(), animated: true)
}else{
//Show alert
}
}
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.navigationController?.pushViewController(NewViewController())
}
}
class CustomView:UIView{
var onTap:(()->Void)?
...
}
class ViewController:UIViewController{
let customView = CustomView()
var buttonClicked = false
func setupCustomView(){
cell.onButtonTap = { [unowned self] in
self.navigationController?.pushViewController(NewViewController(), animated: true)
}
class TableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.onButtonTap = {
self.navigationController?.pushViewController(NewViewController(), animated: true)
}
}
}
class ViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.onButtonTap = {
self.navigationController?.pushViewController(NewViewController(), animated: true)
}
}
}