Skip to content

Instantly share code, notes, and snippets.

var test: Int = 1000
let data = Data(bytes: &test, count: MemoryLayout<Int>.size)
var number: Int = 0
number = data.withUnsafeBytes { (pointer: UnsafePointer<Int>) -> Int in
return pointer.pointee
}
@staticVoidMan
staticVoidMan / FailableConvenienceInit.swift
Created April 20, 2018 05:56
Init: Showing default parameters, a failable init, a convenience init
class Bird {
enum BiologicalSex {
case undetermined
case male
case female
}
var name: String?
var biologicalSex: BiologicalSex
@staticVoidMan
staticVoidMan / Custom Decoder.swift
Last active April 27, 2018 10:08
JSONDecoder: Custom Decoder implementation
import Foundation
struct Result {
enum Level {
case junior
case senior
case enticer
case undetermined(String)
}
@staticVoidMan
staticVoidMan / IrregularArrayDataDecoder.swift
Last active April 20, 2018 09:48
JSONDecoder: Array || Decoding irregular data
enum IrregularData {
case string(String)
case bool(Bool)
case int(Int)
case unknown(Any?)
}
extension IrregularData: Decodable {
@staticVoidMan
staticVoidMan / IrregularDictionaryDataDecoder.swift
Last active April 20, 2018 09:49
JSONDecoder: Dictionary || Decoding irregular data
enum MyData {
case string(String)
case bool(Bool)
case int(Int)
case unknown
}
extension MyData: Decodable {
var animals = ["Dog", "Cat", "Hamster", "Pig"]
var animalTags = [3, 2, 4, 1]
//Zip the two together
let animalsTagsZip = zip(animals, animalTags)
//Sort them based on your requirement to get an array of (String, Int) tuples
let animalsTagsSorted = animalsTagsZip.sorted { (lhs, rhs) -> Bool in
return lhs.1 > rhs.1
}
@staticVoidMan
staticVoidMan / N-Optional.swift
Created April 30, 2018 23:43
Concisely unwrapping multi-optional variables (https://stackoverflow.com/a/33049398/2857130)
let myString: String??? = "Hello, World!"
print(myString!!!)
if case let safelyUnwrappedString??? = myString {
print(safelyUnwrappedString)
}
if let safelyUnwrappedString = myString as? String {
print(safelyUnwrappedString)
@staticVoidMan
staticVoidMan / KeyPath_Basic.swift
Created May 3, 2018 09:58
Basic usage of KeyPaths
//Ref: https://www.klundberg.com/blog/swift-4-keypaths-and-you/
struct Person {
var name: String
}
let aKeyPath = \Person.name
type(of: aKeyPath).rootType //Root type of the keyPath: Person
type(of: aKeyPath).valueType //Value type of the keyPath: String
@staticVoidMan
staticVoidMan / KeyPaths_ExtendedBasics.swift
Created May 3, 2018 09:58
Basics on KeyPaths for nested sequences (with optionals)
struct Person {
var name: String
var likes: [String:[Any]]?
}
let aKeyPath = \Person.likes?["friends"]?[0]
type(of: aKeyPath).rootType
type(of: aKeyPath).valueType
@staticVoidMan
staticVoidMan / ClosureThrows.swift
Created May 3, 2018 10:33
Function that rethrows what it's closure throws
enum MyError: Error {
case RuntimeError(String)
}
func doSomething(_ completion: (Bool) throws -> Void) rethrows {
// Try the completion as the closure could throw
try completion(false)
}
do {