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 / UIViewController+Child.swift
Created December 20, 2021 10:39
Add and remove child view controllers
import UIKit
extension UIViewController {
func add(_ child: UIViewController) {
addChild(child)
view.addSubview(child.view)
child.didMove(toParent: self)
}
func remove() {
@jeandavid
jeandavid / UIResponder+ParentViewController.swift
Created December 20, 2021 10:25
Get the parent view controller by using the UIResponder chain
import UIKit
extension UIResponder {
@objc
public var parentViewController: UIViewController? {
return next as? UIViewController ?? next?.parentViewController
}
}
@jeandavid
jeandavid / UITextView+Placeholder.swift
Created October 20, 2021 08:25
UITextView Placeholder support
import UIKit
// https://stackoverflow.com/a/50671026
extension UITextView {
private class PlaceholderLabel: UILabel { }
private var placeholderLabel: PlaceholderLabel {
guard let label = subviews.first(where: { $0 is PlaceholderLabel }) as? PlaceholderLabel else {
let label = PlaceholderLabel(frame: .zero)
label.font = font
@jeandavid
jeandavid / BundleFinder.swift
Created March 12, 2021 08:02
Accessing Resources from Framework
private class BundleFinder {}
public class MyView {
let imageView = UIImageView()
func setImage() {
let frameworkBundle = Bundle(for: BundleFinder.self)
let image = UIImage(named: "my_image", in: frameworkBundle, with: nil)
imageView.image = image
}
@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
}
}
@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 / 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 / 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 / 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 / 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.