Skip to content

Instantly share code, notes, and snippets.

@hlung
hlung / APIClient.swift
Last active February 21, 2022 01:56
APIClient startDecodableRequest
import Foundation
class APIClient {
// 1 - Error enum
enum APIClientError: Error {
case dataTaskFailed(Error)
case noHTTPURLResponse
case badHTTPStatusCode(HTTPURLResponse)
case noData
div > .vimiumHintMarker {
/* linkhint boxes */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#565756),
color-stop(100%,#242524));
border: 1px solid #e4e5e4;
opacity: 1.0;
text-shadow: none !important;
}
div > .vimiumHintMarker span {
@hlung
hlung / UIColor+PXExtensions.h
Last active December 30, 2020 20:59
Make UIColor support hex color codes
//
// UIColor+PXExtensions.h
// Creates UIColor from hex string. "#" prefix is optional. Supports 3 and 6 hex digits.
// Originally from http://pixelchild.com.au/post/12785987198/how-to-convert-a-html-hex-string-into-uicolor-with
// But that link seems to be broken.
// Revived by Thongchai Kolyutsakul (21 May 2015).
//
// USAGE: UIColor *mycolor = [UIColor pxColorWithHexValue:@"#BADA55"];
// UIColor *mycolor = [UIColor pxColorWithHexValue:@"FFFFFF"];
// UIColor *mycolor = [UIColor pxColorWithHexValue:@"1AD"];
@hlung
hlung / LowestCommonAncestor.swift
Last active October 25, 2020 10:23
Find the least common ancestor of 2 views.
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 July 23, 2020 02:48
Change ownership of all apps in Applications folder to current user
find /Applications -name "*.app" -user old-user -maxdepth 1 | tr \\n \\0 | xargs -0 sudo chown -R $USER
@hlung
hlung / DynamicCodingKey.swift
Last active June 27, 2020 06:39
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.)
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 12:21
Iterate through all elements in pair tuples
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 12:12
Create UIColor from hex string. e.g. UIColor(hex: "EAEAEA")
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 April 21, 2020 03:10
A simple Xcode extension that reverses code on selected lines
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] ?? []
@hlung
hlung / iOS - NSData+hexadecimalString.m
Last active March 19, 2020 08:03
Converts NSData to a hexadecimal string.
/** Converts NSData to a hexadecimal string. */
@interface NSData (NSData_hexadecimalString)
/** Changes NSData object to a hex string.
@returns hexadecimal string of NSData. Empty string if data is empty.*/
- (NSString *)hexadecimalString;
@end
@implementation NSData (NSData_hexadecimalString)