Skip to content

Instantly share code, notes, and snippets.

View jeandavid's full-sized avatar

Jean-David Morgenstern-Peirolo jeandavid

View GitHub Profile
@jeandavid
jeandavid / singleton.swift
Last active July 30, 2018 08:45
Singleton Pattern in Swift
//
// UserSingleton.swift
//
//
// Created by Jean-David Morgenstern-Peirolo on 7/30/18.
//
import Foundation
final class UserSingleton {
@jeandavid
jeandavid / SyncOperation.swift
Last active February 8, 2020 20:11
Synchronous Operation
class SyncOperation: Operation {
override func main() {
print("Executing")
}
}
let operation = SyncOperation()
operation.start()
print("Done")
// Outputs "Executing" then "Done"
@jeandavid
jeandavid / AsyncDispatch.swift
Created February 8, 2020 20:09
Asynchronous Operation
class AsyncOperation: Operation {
override func main() {
DispatchQueue.global().async {
print("Executing")
}
}
}
let operation = AsyncOperation()
operation.start()
print("Done")
@jeandavid
jeandavid / SyncOperationQueue.swift
Created February 8, 2020 20:11
Synchronous Operation Added to a Queue
let syncOperation = SyncOperation()
let queue = OperationQueue()
queue.addOperation(syncOperation)
print("Done")
// May output "Executing" then "Done" or "Done" then "Executing"
@jeandavid
jeandavid / AsyncOperation.swift
Created February 8, 2020 20:13
Asynchronous Operation
class AsyncOperation: Operation {
// Only required when you want to manually start an operation
// Ignored when an operation is added to a queue.
override var isAsynchronous: Bool { return true }
// This is not the OperationQueue!
// This is the queue we use to read and write the operation state in a safe thread way
private let queue = DispatchQueue(label: "async_operation_private_queue", attributes: .concurrent)
// State is accessed and modified in a thread safe and KVO compliant way.
@jeandavid
jeandavid / SerialOperation.swift
Created February 8, 2020 20:16
Serial Executions of two operations
let first = BlockOperation {
print("First")
}
let second = BlockOperation {
print("Second")
}
first.start()
second.start()
print("Done")
// Output:
@jeandavid
jeandavid / ConcurrentOperation.swift
Created February 8, 2020 20:17
Concurrent execution of two operations
let syncOperation = SyncOperation()
let asyncOperation = AsyncOperation()
let queue = OperationQueue()
queue.addOperation(syncOperation)
queue.addOperation(asyncOperation)
@jeandavid
jeandavid / SyncConcurrentOperation.swift
Created February 8, 2020 20:19
Synchronous execution of two operations
let syncOperation = SyncOperation()
let asyncOperation = AsyncOperation()
let queue = OperationQueue()
queue.addOperations([syncOperation, asyncOperation], waitUntilFinished: true)
print("Done")
// Outputs "Executing" twice then "Done"
@jeandavid
jeandavid / LinkableLabel.swift
Created February 14, 2021 11:22
A UILabel that responds to embedded link tap
import UIKit
/// A UILabel subclass that responds to links tap.
public class LinkableLabel: UILabel {
// MARK: - Private
private var touchedLink: URL?
@objc
@jeandavid
jeandavid / MyView.swift
Last active March 12, 2021 08:01
Accessing Resources from Embedded Framework Bundle
public class MyView {
let imageView = UIImageView()
func setImage() {
let frameworkBundle = Bundle(identifier: "com.myframework")
let image = UIImage(named: "my_image", in: frameworkBundle, with: nil)
imageView.image = image
}
}