Skip to content

Instantly share code, notes, and snippets.

@dkw5877
dkw5877 / StackViewController.swift
Last active April 12, 2019 21:54
Example of UIViewController who's main view has been replaced by a UIStackView
import UIKit
final class StackViewController: UIViewController {
private let stackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fill
@dkw5877
dkw5877 / ContainerViewController.swift
Created April 12, 2019 20:03
Example of a basic container view controller
import UIKit
final class ContainerViewController: UIViewController {
private let imageController:UIViewController
private let listController:UIViewController
init(imageController:UIViewController, listController:UIViewController) {
self.imageController = imageController
@dkw5877
dkw5877 / CollectionEqualityCustom.swift
Created March 28, 2019 17:39
Example of testing equality on collections in Swift with custom structs
enum AnimalType {
case tiger
case elephant
case otter
}
struct Animal:Equatable {
let name:String
let type:AnimalType
}
@dkw5877
dkw5877 / CollectionEquality.swift
Created March 28, 2019 17:37
Testing equality in collections in Swift
let strings1 = ["a", "b", "c"]
let strings2 = ["a", "b", "c"]
var isEqual = strings1 == strings2 //true
let strings1 = ["a", "b", "c"]
let strings2 = ["a", "b", "d"]
var isEqual = strings1 == strings2 //false
let strings1 = ["a", "b", "c"]
let strings2 = ["b", "c", "a"]
@dkw5877
dkw5877 / AVMakeRect.swift
Created March 28, 2019 17:19
Example of using AVFoundation's AVMakeRect to resize and image
/* create the image at original size */
let image = UIImage(named: "Landscape-1276x800.jpg")!
/*
* create bounding rect with largest possible height based on width
* this will get you the aspect height for the speficied width
*/
let boundingRect = CGRect(x: 0, y: 0, width: 300, height:CGFloat.greatestFiniteMagnitude)
/*
@dkw5877
dkw5877 / DateDecodingStrategy.swift
Last active March 26, 2019 23:14
Date Decoding Strategy Key Encoding and Decoding in Swift
struct TestDecode:Codable {
let updated:Date
}
let json = """
{
"updated":"2019-2-21T02:02:55-08:00"
}
"""
@dkw5877
dkw5877 / Base64Encoding.swift
Created March 26, 2019 23:08
Base 64 Key Encoding and Decoding in Swift
let dates = ["key_1":Date()]
let encoder = JSONEncoder()
encoder.dataEncodingStrategy = .base64
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
let encoded = try? encoder.encode(object)
print(String(data: encoded!, encoding: .utf8)!)
//output
{
"key_1" : 574821298.18860102
@dkw5877
dkw5877 / KeyEncodingStrategy.swift
Created March 26, 2019 23:07
Key Encoding and Decoding in Swift
let object = ["keyOne":"Bengal tiger", "keyTwo":"Siberian tiger", "keyThree ":"white rhino", "keyFour":"African elephant"]
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
let encoded = try? encoder.encode(object)
print(String(data: encoded!, encoding: .utf8)!)
//output
{
"key_four" : "African elephant",
@dkw5877
dkw5877 / PrettyPrint2.Swift
Created March 26, 2019 23:05
Key Encoding and Decoding in Swift
let object = ["key1":"Bengal tiger", "key2":"Siberian tiger", "key3 ":"white rhino", "key4":"African elephant"]
let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
let encoded = try? encoder.encode(object)
print(String(data: encoded!, encoding: .utf8)!)
//output
{
"key1" : "Bengal tiger",
"key2" : "Siberian tiger",
@dkw5877
dkw5877 / PrettyPrint.swift
Last active March 29, 2019 14:23
Key Encoding in Swift
let object = ["key1":"bengal tiger", "key2":"siberian tiger", "key3":"white rhino", "key4":"african elephant"]
let encoder = JSONEncoder()
encoder.outputFormatting= .prettyPrinted
let encoded = try? encoder.encode(object)
print(String(data: encoded!, encoding: .utf8)!)
//output
{
"key3" : "white rhino",
"key1" : "bengal tiger",