Skip to content

Instantly share code, notes, and snippets.

@yakushevichsv
yakushevichsv / VisionFaceTracker.swift
Created September 28, 2021 18:35
Contains code for detecting & tracking face, and approximate blink calculation.
//
// VisionFaceTracker.swift
// Test
//
// Created by Siarhei Yakushevich on 28.09.21.
//
import Foundation
import Vision
@yakushevichsv
yakushevichsv / CIFaceFilter.swift
Created September 26, 2021 15:37
Allows to detect smile & blink using CoreImage
//
// CIFaceFilter.swift
//
import Foundation
import CoreImage
import CoreGraphics
// MARK: - FaceFilterFeatures
struct FaceFilterFeatures {
@yakushevichsv
yakushevichsv / Optional_FlatMap_Example.swift
Created September 22, 2021 19:42
Simple sample of using Map & FlatMap On Optionals
var str: String?
let result0 = str.flatMap { Int($0) }.valueOrDefault(.min)
var result1: Int = .min
if let str = str {
result1 = Int(str) ?? .min
}
// result1 can be changed here, but result0 can't.
assert(result0 == result1)
@yakushevichsv
yakushevichsv / String+Empty.swift
Created September 22, 2021 19:23
Empty String
extension String {
static let empty = "" // swift 5.5 allows static let constructs in extension
#if DEBUG
func check() {
assert(Self.empty == "")
}
#endif
}
@yakushevichsv
yakushevichsv / Optional+Ext.swift
Created September 22, 2021 18:31
Contains extensions on Optional type (DSL wrappers)
import Foundation
extension Optional {
var hasValue: Bool {
if case .some = self {
return true
}
return false
}
@yakushevichsv
yakushevichsv / AtomicPropertyWrapper.swift
Created September 5, 2021 09:41
Atomic Property Wrapper Using Unfair Lock
// MARK: - Atomic
@propertyWrapper struct Atomic<T> {
private let lock = UnfairLock()
private var value: T
var wrappedValue: T {
get {
lock.lock()
defer {
@yakushevichsv
yakushevichsv / UnfairLock.swift
Created September 5, 2021 08:53
os_unfair_lock_t instead of NS{Recursive}Lock
// MARK: - UnfairLock
final class UnfairLock {
private let unfairLock: os_unfair_lock_t //UnsafeMutablePointer<os_unfair_lock>
private let unfairValue: os_unfair_lock_s
init() {
unfairLock = .allocate(capacity: 1)
unfairValue = .init()
unfairLock.initialize(to: unfairValue)
}
@yakushevichsv
yakushevichsv / DispatchQueue_getSpecific_test.swift
Created August 29, 2021 16:14
Test code for checking DispatchQueue.getSpecific method
final class Test {
private let queue: DispatchQueue
private let queueKey : DispatchSpecificKey<Int>
private let queueKeyValue: Int
init() {
let queueKey = DispatchSpecificKey<Int>()
let queueKeyValue = Int(arc4random())
self.queueKey = queueKey
@yakushevichsv
yakushevichsv / UILabel+Multiline.swift
Created July 31, 2021 09:23
Kind of vertical stack view & Extensions
import UIView
extension UILabel {
func markAsMultiline() {
numberOfLines = 0
lineBreakMode = .byWordWrapping
}
}
@yakushevichsv
yakushevichsv / MainThreadRunnerType+Sample.swift
Last active June 21, 2021 19:19
Contains sample of MainThreadRunnerType's usage
class Test1: MainThreadRunnerType {
func displayMessage(_ text: String) {
debugPrint(text)
}
func displayMessage(optText: String?) {
guard let text = optText else { return }
displayMessage(text)
}