Skip to content

Instantly share code, notes, and snippets.

@RustyKnight
RustyKnight / ScannerHelper.java
Created November 8, 2021 01:39
Java Scanner prompt helper
import java.util.Scanner;
/*
I see to often people using Scanner to get user input and just, plainly, screwing it.
People don't seem to realise that they can use multiple Scanners to make life easier
for themselves, for example, the primary Scanner can just be used to get the next line
of text input by the user, then a secondary Scanner can be used to parse the input
based on their needs. The following is a simple example to prompt the user for a int
value, with a optional exit value and error message
*/
@RustyKnight
RustyKnight / UIButtonSelected.swift
Created September 12, 2021 03:35
Simple playground testing .isSelected property to change image
import UIKit
import PlaygroundSupport
final class MyViewController: UIViewController {
let button = UIButton(type: .custom)
override func loadView() {
let view = UIView()
view.backgroundColor = .green
@RustyKnight
RustyKnight / TestDefaults.swift
Created September 11, 2021 23:44
Testing UserDefaults set(nil, forKey:) and removeObject(forKey:) to see size difference in the plist store
import UIKit
var greeting = "Hello, playground"
let key = "Test"
let value = "test".data(using: .utf8)!
let defaults = UserDefaults.standard
func printUD() {
print("UserDefaults after modification:\n")
defaults.dictionaryRepresentation().forEach { print("\($0): \($1)\n") }
@RustyKnight
RustyKnight / String+Encode.swift
Created August 30, 2018 22:17
A simple reminder to myself about how to encode a string from encoding to another
extension String {
func encode(from: String.Encoding, to: String.Encoding) -> String? {
guard let originalData = data(using: from) else {
return nil
}
return String(data: originalData, encoding: to)
}
}
@RustyKnight
RustyKnight / Device.swift
Created August 29, 2018 22:11
A customisation and collection of various "device" related operations and checks
import Foundation
import UIKit
struct Device {
// MARK: - Singletons
static var shared: UIDevice {
struct Singleton {
@RustyKnight
RustyKnight / UIViewController+Alert.swift
Created August 29, 2018 22:09
Extension to provide simpler support for showing UIAlertController is "common" scenarios
import Foundation
import UIKit
extension UIViewController {
@objc func presentErrorAlertWith(message: String,
preferredStyle: UIAlertController.Style = .alert,
handler: AlertActionHandler? = nil) {
guard Thread.isMainThread else {
@RustyKnight
RustyKnight / UIAlertAction+Options.swift
Last active August 29, 2018 22:08
Common UIAlertAction options - more of a template
import Foundation
import UIKit
typealias AlertActionHandler = (_ action: UIAlertAction) -> Void
extension UIAlertAction {
static var okay: UIAlertAction {
return UIAlertAction(title: "OK", style: .default, handler: nil)
}
@RustyKnight
RustyKnight / UIView+FirstResponder.swift
Created August 29, 2018 22:07
Extension to find which view is currently the first responder - this only works if the responder is the current view or a child of the current view
import Foundation
import UIKit
extension UIView {
var firstResponder: UIView? {
guard !isFirstResponder else {
return self
}
@RustyKnight
RustyKnight / UIView+FirstResponder.swift
Created August 29, 2018 22:06
Extension to find which view is currently the first responder - this only works if the responder is the current view or a child of the current view
import Foundation
import UIKit
extension UIView {
var firstResponder: UIView? {
guard !isFirstResponder else {
return self
}
@RustyKnight
RustyKnight / UIViewController+KeyboardDisplacer.swift
Created August 29, 2018 22:05
Extension to display the view when keyboard is presented
import Foundation
import UIKit
// I'm not a fan of this, but this will allow us to maintain state beyond the capabailities of the the extension API
fileprivate var cache = NSHashTable<UIViewController>(options: .weakMemory)
extension UIViewController {
fileprivate var isInstalled: Bool {