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 | |
extension UIViewController { | |
func add(_ child: UIViewController) { | |
addChild(child) | |
view.addSubview(child.view) | |
child.didMove(toParent: self) | |
} | |
func remove() { |
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 | |
extension UIResponder { | |
@objc | |
public var parentViewController: UIViewController? { | |
return next as? UIViewController ?? next?.parentViewController | |
} | |
} |
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 | |
// 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 |
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
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 | |
} |
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
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 | |
} | |
} |
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 | |
/// A UILabel subclass that responds to links tap. | |
public class LinkableLabel: UILabel { | |
// MARK: - Private | |
private var touchedLink: URL? | |
@objc |
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
let syncOperation = SyncOperation() | |
let asyncOperation = AsyncOperation() | |
let queue = OperationQueue() | |
queue.addOperations([syncOperation, asyncOperation], waitUntilFinished: true) | |
print("Done") | |
// Outputs "Executing" twice then "Done" |
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
let syncOperation = SyncOperation() | |
let asyncOperation = AsyncOperation() | |
let queue = OperationQueue() | |
queue.addOperation(syncOperation) | |
queue.addOperation(asyncOperation) |
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
let first = BlockOperation { | |
print("First") | |
} | |
let second = BlockOperation { | |
print("Second") | |
} | |
first.start() | |
second.start() | |
print("Done") | |
// Output: |
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 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. |
NewerOlder