Skip to content

Instantly share code, notes, and snippets.

View pilky's full-sized avatar

Martin Pilkington pilky

View GitHub Profile
@pilky
pilky / KeyValueMap.swift
Created February 17, 2017 17:24
Key value mapping for swift dictionaries
import UIKit
extension Dictionary {
init<S: Sequence>(_ pairs: S) where S.Iterator.Element == (Key, Value) {
self = [:]
for (k,v) in pairs { self[k] = v }
}
func keyValueMap<T: Hashable, S>(_ transform: (Key, Value) throws -> (T, S)) rethrows -> [T: S] {
var newDict = [T: S]()
@pilky
pilky / URLSessionProtocol.swift
Created February 28, 2017 10:45
Swift compiler oddity
protocol URLSessionProtocol {
func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTaskProtocol
}
protocol URLSessionDataTaskProtocol {
func resume()
}
extension URLSessionDataTask: URLSessionDataTaskProtocol {}
@property (assign, nonatomic) CGSize preferredMaxSize;
- (CGSize)intrinsicContentSize {
CGSize imageSize = self.image.size;
CGSize maxSize = self.preferredMaxSize;
if (imageSize.height > maxSize.height) {
imageSize.width *= maxSize.height / imageSize.height;
imageSize.height = maxSize.height;
}
//Obj-C
typedef NSString * ABCMessageField NS_STRING_ENUM;
extern ABCMessageField const ABCMessageFieldSubject;
extern ABCMessageField const ABCMessageFieldFrom;
//Expected Swift (according to https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID17)
struct ABCMessageField: RawRepresentable {
typealias RawValue = String
init(rawValue: RawValue)
@pilky
pilky / gist:1b562464f0ad3a5f268bd0bee8581c63
Created July 27, 2019 15:12
Xcode 11 Playground Crash
import Cocoa
class Foo: NSObject {
var objectID = UUID()
}
let foo = Foo() // Crashes with "error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x20)."
protocol Test {
var foo: String { get set }
func updateFoo()
}
extension Test where Self: NSObject {
func updateFoo() {
self.foo = "Bar" // <- Error: Cannot assign to property: 'self' is immutable
}
class Foo<T> {
struct Bar {
let handler: (T) -> Void
}
var bars: [Bar]
}
@pilky
pilky / optional-weirdness.swift
Created October 28, 2019 14:30
Optional KeyPath Weirdness
import Cocoa
class Foo {
var string = "test"
var optionalString: String?
}
let foo = Foo()
foo[keyPath: \.string] == nil //false
@pilky
pilky / CalculatingCellSize.swift
Created March 16, 2020 17:22
Dynamic NSOutlineView row heights
class MyOutlineDelegate: NSObject, NSOutlineViewDelegate {
//The cell we'll use for calculating sizes, should be the same cell used in the outline
lazy var sizingCell: MyCell = {
let cell = MyCell(frame: .zero)
cell.translatesAutoresizingMaskIntoConstraints = false
return cell
}()
lazy var sizingCellWidthAnchor: NSLayoutConstraint = {
return self.sizingCell.widthAnchor.constraint(equalToConstant: 1000)
@pilky
pilky / ExtendableFilePromiseProvider.swift
Created May 25, 2020 12:28
NSFilePromiseProvider that supports additional types
class ExtendableFilePromiseProvider: NSFilePromiseProvider {
var additionalItems: [NSPasteboard.PasteboardType: Any] = [:]
override func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
var types = super.writableTypes(for: pasteboard)
if self.additionalItems.count > 0 {
types.append(contentsOf: self.additionalItems.keys)
}
return types
}