Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
SlappyAUS / httpRequest.swift
Last active October 9, 2020 23:50
HTTP Request - Making Request #network #http
// Apply Delegate to the UIViewController
protocol CoinManagerDelegate {
func didUpdateData(_ sender: CoinManager, parsedData: BitcoinData)
func didFailWithError(_ sender: CoinManager, error: Error)
}
// Insert these calls into the component making the call (Manager class)
func performRequest(with urlString: String) {
let url = URL(string: urlString)
@SlappyAUS
SlappyAUS / UpdateUI.swift
Last active October 9, 2020 23:50
Update UI on Main Thread #ui
DispatchQueue.main.async {
// Update UI components here
}
@SlappyAUS
SlappyAUS / timer.swift
Last active October 9, 2020 23:50
Timer with Closure #util
Timer.scheduledTimer(withTimeInterval: interval, repeats: false) { (timer) in
// Code here
}
@SlappyAUS
SlappyAUS / firebase-user.swift
Last active October 9, 2020 23:50
User Management #firebase
// App Delegate
import Firebase
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
FirebaseApp.configure()
return true
}
// Register User Controller
@SlappyAUS
SlappyAUS / controller.swift
Last active October 9, 2020 23:50
Register Custom XIB Cell with TableView #ui
override func viewDidLoad() {
super.viewDidLoad()
// Register XIB for custom cell
tableView.register(UINib(nibName: K.cellNibName, bundle: nil), forCellReuseIdentifier: K.cellIdentifier)
}
@SlappyAUS
SlappyAUS / firestore.swift
Last active October 9, 2020 23:50
FireStore #firebase
// App Deleagate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
_ = Firestore.firestore()
return true
}
// Controller
// Init Firestore
@SlappyAUS
SlappyAUS / IQKeyboardManager.swift
Last active October 9, 2020 23:50
IQKeyboardManager #ui
// App Delegate
import IQKeyboardManagerSwift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
IQKeyboardManager.shared.enable = true
IQKeyboardManager.shared.enableAutoToolbar = false
return true
}
@SlappyAUS
SlappyAUS / TableViewDataSource.swift
Last active October 9, 2020 23:49
TableViewDataSource #ui
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)
cell.textLabel?.text = itemArray[indexPath.row]
return cell
}
@SlappyAUS
SlappyAUS / alertWithTextfield.swift
Last active October 9, 2020 23:49
Alert with TextField #ui
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "Add New Todoey Item", message: "", preferredStyle: .alert)
var textField: UITextField? = nil
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
if let safeTextField = textField {
print(safeTextField.text ?? "")
}
}
@SlappyAUS
SlappyAUS / userDefaults.swift
Created October 8, 2020 00:09
UserDefaults #data
// Controller
let defaults = UserDefaults.standard
// Reading
if let items = defaults.array(forKey: "TodoListArray") as? [String] {
itemArray = items
}
else {
itemArray = []