This file contains hidden or 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
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 | |
*/ |
This file contains hidden or 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
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") } |
This file contains hidden or 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 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) | |
} | |
} |
This file contains hidden or 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
import Foundation | |
import UIKit | |
struct Device { | |
// MARK: - Singletons | |
static var shared: UIDevice { | |
struct Singleton { |
This file contains hidden or 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
import Foundation | |
import UIKit | |
extension UIViewController { | |
@objc func presentErrorAlertWith(message: String, | |
preferredStyle: UIAlertController.Style = .alert, | |
handler: AlertActionHandler? = nil) { | |
guard Thread.isMainThread else { |
This file contains hidden or 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
import Foundation | |
import UIKit | |
typealias AlertActionHandler = (_ action: UIAlertAction) -> Void | |
extension UIAlertAction { | |
static var okay: UIAlertAction { | |
return UIAlertAction(title: "OK", style: .default, handler: nil) | |
} |
This file contains hidden or 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
import Foundation | |
import UIKit | |
extension UIView { | |
var firstResponder: UIView? { | |
guard !isFirstResponder else { | |
return self | |
} |
This file contains hidden or 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
import Foundation | |
import UIKit | |
extension UIView { | |
var firstResponder: UIView? { | |
guard !isFirstResponder else { | |
return self | |
} |
This file contains hidden or 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
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 { |