Skip to content

Instantly share code, notes, and snippets.

View HitTestOverride.swift
// Put this in a UITableViewCell or UICollectionViewCell subclass.
// Returns nil if point is not within the specified subviews.
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let subviews = [imageView, titleLabel, detailLabel, downloadButton]
for view in subviews {
if view.convert(view.bounds, to: self).contains(point) {
return super.hitTest(point, with: event)
}
}
return nil
View Dynamic cell height in UICollectionViewCompositionalLayout.swift
let layout = UICollectionViewCompositionalLayout(sectionProvider: {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
View CompositionalLayoutCenterAlign-1.swift
let layout = UICollectionViewCompositionalLayout(sectionProvider: {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
// (1) setting widthDimension to half of collectionView size
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5),
heightDimension: .absolute(50))
@hlung
hlung / APIClient.swift
Last active Feb 21, 2022
APIClient startDecodableRequest
View APIClient.swift
import Foundation
class APIClient {
// 1 - Error enum
enum APIClientError: Error {
case dataTaskFailed(Error)
case noHTTPURLResponse
case badHTTPStatusCode(HTTPURLResponse)
case noData
@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] ?? []