This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
receiveItems { //apparantly the method's declaration is that: receiveItems(completion: ()-> Void) | |
//do some processing in completion block | |
let items = ... | |
DispatchQueue.main.async { [weak self] in | |
// update ui | |
guard let self = self else { return } | |
self.items = items | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// MARK: - MainThreadRunnerType | |
protocol MainThreadRunnerType { | |
func runOnMain(_ block: @escaping () -> Void) // if not on main thread then run block asynchronously | |
func runOnMainWeakly<Object: AnyObject, T>(_ obj: Object, | |
parameter: T, | |
method: @escaping ((Object) -> (T) -> Void)) | |
func runOnMainWeakly<Object: AnyObject>(_ obj: Object, | |
method: @escaping ((Object) -> () -> Void)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension MainThreadRunnerType { | |
static func weakify<Object: AnyObject, Input>(_ obj: Object, | |
method: @escaping ((Object) -> (Input) -> Void)) -> ((Input) -> Void) { | |
return { [weak obj] value in | |
guard let obj = obj else { return } | |
method(obj)(value) | |
} | |
} | |
static func weakify<Object: AnyObject>(_ obj: Object, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test1: MainThreadRunnerType { | |
func displayMessage(_ text: String) { | |
debugPrint(text) | |
} | |
func displayMessage(optText: String?) { | |
guard let text = optText else { return } | |
displayMessage(text) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIView | |
extension UILabel { | |
func markAsMultiline() { | |
numberOfLines = 0 | |
lineBreakMode = .byWordWrapping | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Atomic | |
@propertyWrapper struct Atomic<T> { | |
private let lock = UnfairLock() | |
private var value: T | |
var wrappedValue: T { | |
get { | |
lock.lock() | |
defer { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Optional { | |
var hasValue: Bool { | |
if case .some = self { | |
return true | |
} | |
return false | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
static let empty = "" // swift 5.5 allows static let constructs in extension | |
#if DEBUG | |
func check() { | |
assert(Self.empty == "") | |
} | |
#endif | |
} |
OlderNewer