View LowestCommonAncestor.swift
import Foundation | |
import UIKit | |
// From an example view hierarchy... | |
// 1 -> 2 -> 3 -> 4 -> 5 | |
// | | |
// ---> 6 | |
// Find the least common ancestor of 2 views. | |
// Input |
View chown-applications.sh
find /Applications -name "*.app" -user old-user -maxdepth 1 | tr \\n \\0 | xargs -0 sudo chown -R $USER |
View DynamicCodingKey.swift
import Foundation | |
// Allows defining CodingKey from String | |
struct DynamicCodingKey: CodingKey { | |
var intValue: Int? | |
var stringValue: String | |
init?(intValue: Int) { | |
assertionFailure("Not implemented") | |
return nil |
View Array+Pairs.swift
import Foundation | |
public extension Array { | |
// Iterate through all elements in pair tuples | |
// e.g. [1, 2, 3, 4].allPairs = [(1, 2), (2, 3), (3, 4)] | |
var allPairs: [(Element, Element)] { | |
var array: [(Element, Element)] = [] | |
for i in 0..<self.count - 1 { | |
array.append((self[i], self[i+1])) | |
} |
View UIColor+Hex.swift
import UIKit | |
public extension UIColor { | |
convenience init(hex: String) { | |
let r, g, b, a: CGFloat | |
var hex = hex | |
if hex.hasPrefix("#") { hex = String(hex.dropFirst()) } | |
if hex.count == 6 { |
View XcodeExtReverseLines.swift
import Foundation | |
import XcodeKit | |
class SourceEditorCommand: NSObject, XCSourceEditorCommand { | |
// Reverses code on selected lines | |
func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: @escaping (Error?) -> Void ) -> Void { | |
let lines = invocation.buffer.lines as? [String] ?? [] |
View medium-codable-response-wrapper.swift
let data = """ | |
{ | |
"response": [ | |
{ | |
"name": "Kitty", | |
}, | |
{ | |
"name": "Doggy", | |
} | |
] |
View medium-codable-animal.swift
let exampleData = """ | |
[ | |
{ | |
"type": "cat", | |
"name": "Kitty", | |
}, | |
{ | |
"type": "dog", | |
"name": "Doggy", | |
} |
View medium-codable-lossyarray.swift
struct LossyArray<Element: Decodable>: Decodable { | |
private(set) var elements: [Element] | |
init(from decoder: Decoder) throws { | |
var container = try decoder.unkeyedContainer() | |
var elements = [Element]() | |
if let count = container.count { | |
elements.reserveCapacity(count) | |
} |
NewerOlder