Skip to content

Instantly share code, notes, and snippets.

protocol ThingsServiceDelegate {
func service(_ ThingsService, didFinishWithResult: Result<[Thing]>)
}
class ThingsService {
private var delegate: ThingsServiceDelegate
init(with delegate: ThingsServiceDelegate) {
self.delegate = delegate
}
func getThings(onCompletion callback: (Result<[Thing]>) -> Void) { ... }
let successResult: Result<[Thing]> = .success([aThing, anotherThing])
let failureResult: Result<[Thing]> = .failure(Error(...))
let successResult: ([Thing]?, Error?) = ([aThing, anotherThing], nil)
let failureResult: ([Thing]?, Error?) = (nil, Error(...))
let service = ThingsService()
activityIndicator.startAnimating()
service.getThings { getThingsResult in
activityIndicator.stopAnimating()
updateUI(with: getThingsResult)
}
let syncService = SyncronicThingsService()
activityIndicator.startAnimating()
DispatchQueue.global(qos: .background).async {
let getThingsResult = syncService.getThings()
DispatchQueue.main.async {
activityIndicator.stopAnimating()
updateUI(with: getThingsResult)
}
}
class Tap {
private static var instances: [Tap] = []
// ...
static func on(view: UIView, fires action: @escaping () -> Void) {
let tap = Tap(view: view, action: action)
Tap.instances.append(tap)
}
func aMethod() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTap(_:)))
aView.addGestureRecognizer(tapGesture)
}
@objc func onTap(_ gesture: UIGestureRecognizer) {
if (gesture.state == .ended) {
/* action */
}
}
func aMethod() {
aView.onTap { /* action */ }
// or
Tap.on(aView) { /* action */ }
}
class Tap {
private var view: UIView
private var action: () -> Void
init(view: UIView, action: @escaping () -> Void) {
self.view = view
self.action = action
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTap(_:)))
view.addGestureRecognizer(tapGesture)