Skip to content

Instantly share code, notes, and snippets.

@SlappyAUS
SlappyAUS / camera_and_photo_selector.swift
Last active October 17, 2020 09:11
Camera and photo selector #ui
//
// ViewController.swift
// WhatFlower
//
// Created by Greg Eales on 17/10/20.
//
import UIKit
// TODDO: Add to .plist
@SlappyAUS
SlappyAUS / coreData.swift
Last active October 9, 2020 23:51
CoreData #data #coreData
// App Delegate
import CoreData
func applicationWillTerminate(_ application: UIApplication) {
self.saveContext()
}
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
@SlappyAUS
SlappyAUS / propertyList.swift
Created October 8, 2020 09:56
Property List #data
// Mark "Item" struct as Codable
var itemArray: [Item] = []
let dataFilePath =
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
.first?
.appendingPathComponent("Items.plist")
func loadModel() {
if let data = try? Data(contentsOf: dataFilePath!) {
@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 = []
@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 / 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 / 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 / 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 / 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 / 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