This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)")} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Alamofire.request("https://yourapi.com/login", method: .post, parameters: ["email":"[email protected]","password":"1234"]).responseJSON { (response:DataResponse<Any>) in | |
if response.response?.statusCode == 200 { | |
self.navigationController?.pushViewController(NewViewController(), animated: true) | |
}else{ | |
//Show alert | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { | |
self.navigationController?.pushViewController(NewViewController()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomView:UIView{ | |
var onTap:(()->Void)? | |
... | |
} | |
class ViewController:UIViewController{ | |
let customView = CustomView() | |
var buttonClicked = false | |
func setupCustomView(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cell.onButtonTap = { [unowned self] in | |
self.navigationController?.pushViewController(NewViewController(), animated: true) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
NewerOlder