If we put export
in front of a named entity inside a module, it becomes a named export of that module. All other entities are private to the module.
//===== lib1.mjs =====
// Named exports
import Foundation | |
import InterposeKit | |
import OSLog | |
/// Hack tow work around Assertion failure in -[NSTouchBarLayout setLeadingWidgetWidth:], NSTouchBarLayout.m:78 | |
/// This sometimes happens when macOS restores a window. | |
/// This even runs if there is no OS-level touch bar. | |
class MacOSWorkarounds { | |
static let logger = Logger(category: "MacOSWorkarounds") |
struct Foo: Codable { | |
let type: String | |
let value: String | |
} | |
struct Bar: Codable { | |
let type: String | |
let value: Int | |
} | |
protocol Item { } |
Foundation offers a Thread class, internally based on pthread
, that can be used to create new threads and execute closures.
// Detaches a new thread and uses the specified selector as the thread entry point.
Thread.detachNewThreadSelector(selector: Selector>, toTarget: Any, with: Any)
// Subclass
class MyThread: Thread {
// A loading spinner. | |
// By @marcedwards from @bjango. | |
void setup() { | |
size(300, 300, P2D); | |
frameRate(30); | |
smooth(8); | |
noFill(); | |
stroke(255); | |
strokeWeight(6); |
The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).
My take-aways are:
You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.
Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse
// UICollectionView Objective-C example | |
- (void)viewWillAppear:(BOOL)animated { | |
[super viewWillAppear:animated]; | |
NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] firstObject]; | |
if (selectedIndexPath != nil) { | |
id<UIViewControllerTransitionCoordinator> coordinator = self.transitionCoordinator; | |
if (coordinator != nil) { | |
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { |
import UIKit | |
import Cartography // https://github.com/robb/Cartography | |
/** | |
This is an example of self sizing `UICollectionView` cells using AutoLayout, | |
where the width of cells is always the width of the parent, to mimic `UITableView`. | |
*/ | |
fileprivate let items: [String] = (0..<100) | |
.map { _ in Lorem.sentences(Int.random(min: 1, max: 8)) } // Using https://github.com/lukaskubanek/LoremSwiftum/blob/master/Sources/LoremSwiftum.swift |
import Foundation | |
public struct InfiniteIterator<Base: Collection>: IteratorProtocol { | |
private let collection: Base | |
private var index: Base.Index | |
public init(collection: Base) { | |
self.collection = collection | |
self.index = collection.startIndex |
function run(){ | |
var shouldRun = confirm("Are you sure you want to accept all suggestions?\n\n(Note: Depending on the number of suggestions you have, this script may take a few seconds to run.)"); | |
if(!shouldRun) return false; | |
var events = [ "mouseover", "mousedown", "click", "mouseup" ]; | |
var suggestionButtons = Array.from(document.querySelectorAll("[role='button'][aria-label='Accept suggestion']")); | |
suggestionButtons.forEach((el) => { | |
events.forEach((eventName, i) => { |