Skip to content

Instantly share code, notes, and snippets.

Avatar

Thongchai Kolyutsakul hlung

View GitHub Profile
@hlung
hlung / LowestCommonAncestor.swift
Last active Oct 25, 2020
Find the least common ancestor of 2 views.
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
@hlung
hlung / chown-applications.sh
Last active Jul 23, 2020
Change ownership of all apps in Applications folder to current user
View chown-applications.sh
find /Applications -name "*.app" -user old-user -maxdepth 1 | tr \\n \\0 | xargs -0 sudo chown -R $USER
@hlung
hlung / DynamicCodingKey.swift
Last active Jun 27, 2020
Decodes multiple layers of nested containers using a String array for key path. (It's actually cleaner to declare another struct for the nested data. But this is to show how this can be done.)
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
@hlung
hlung / Array+Pairs.swift
Created May 3, 2020
Iterate through all elements in pair tuples
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]))
}
@hlung
hlung / UIColor+Hex.swift
Created May 3, 2020
Create UIColor from hex string. e.g. UIColor(hex: "EAEAEA")
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 {
@hlung
hlung / XcodeExtReverseLines.swift
Last active Apr 21, 2020
A simple Xcode extension that reverses code on selected lines
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 SwiftUI-ButtonModal
@State private var showModal = false
var body: some View {
Button(action: { self.showModal.toggle() }) {
HStack {
Text("Show MODAL")
Spacer()
Text("〉")
}
}.sheet(isPresented: $showModal) {
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)
}