Skip to content

Instantly share code, notes, and snippets.

@hlung
hlung / CIFilter-colorAbsoluteDifference-demo.swift
Created July 21, 2023 11:22
Demonstrate how to use CIFilter.colorAbsoluteDifference() on macOS
import Cocoa
import CoreImage
import CoreImage.CIFilterBuiltins
let nextButtonImage = NSImage(named: "next button")!
let startGameButtonImage = NSImage(named: "start game button")!
computeImageDifference(inputImage: nextButtonImage, inputImage2: startGameButtonImage)
// MARK: -
@hlung
hlung / RecursiveLazyDependency.swift
Created September 25, 2022 15:15
for medium blog
import UIKit
class MyViewController: UIViewController {
var name: String = "" {
didSet {
print("name = \(name)")
testLabel.text = name // 2
}
}
// 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
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))
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 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
@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]))
}