Skip to content

Instantly share code, notes, and snippets.

View tonyarnold's full-sized avatar

Tony Arnold tonyarnold

View GitHub Profile
@tonyarnold
tonyarnold / MutableCollection+Move.swift
Last active April 28, 2018 15:51
Swift extension that allows moving between a single index and another index in a mutable collection
// The MIT License (MIT)
//
// Copyright © 2018 Tony Arnold (@tonyarnold)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
@tonyarnold
tonyarnold / UIKitCatchupToAppKit.swift
Last active July 16, 2021 16:10
Bring some of AppKit's strongly typed names and identifiers to UIKit
import UIKit
public extension UIStoryboard {
public struct SceneDescriptor {
public let name: UIStoryboard.Name
public let identifier: UIStoryboard.SceneIdentifier
public init(name: UIStoryboard.Name, identifier: UIStoryboard.SceneIdentifier) {
self.name = name
@tonyarnold
tonyarnold / json.swift
Created November 13, 2017 23:41 — forked from rnapier/json.swift
Generic JSON Decodable
enum JSON: Decodable, CustomStringConvertible {
var description: String {
switch self {
case .string(let string): return "\"\(string)\""
case .number(let double):
if let int = Int(exactly: double) {
return "\(int)"
} else {
return "\(double)"
}
@tonyarnold
tonyarnold / UserDefaults.swift
Last active September 21, 2017 23:49
Adds subscripting to `Foundation.UserDefaults`, and defines a type-safe key type
//
// Copyright © 2017 Tony Arnold.
//
// This code is licensed under the MIT license.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERW
//
// Copyright © 2017 Tony Arnold. All rights reserved.
enum CompoundValue<Element: Hashable> {
case distinct(Element)
case indistinct(Set<Element>)
case empty
var distinctValue: Element? {
switch self {
extension RangeReplaceableCollectionType where Index : Comparable {
mutating func removeAtIndexes<S : SequenceType where S.Generator.Element == Index>(indexes: S) -> [Self.Generator.Element] {
return indexes.sort().lazy.reverse().map { removeAtIndex($0) }
}
}
@tonyarnold
tonyarnold / CALayerWTF.md
Last active November 17, 2019 23:13
Am I misunderstanding how CALayer's contentsCenter property works?
NSEdgeInsets capInsets = backgroundImage.capInsets;
NSSize imageSize = backgroundImage.size;

CALayer *layer = someView.layer;
layer.contents = backgroundImage;
layer.contentsScale = self.backingScaleFactor;
if (someView.isFlipped)
{
 layer.contentsCenter = CGRectMake(
#ifndef MagicalRecordXcode7CompatibilityMacros_h
#define MagicalRecordXcode7CompatibilityMacros_h
#if __has_feature(nullability)
#define MR_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
#define MR_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
#define MR_nullable nullable
#define MR_nonnull nonnull
#define __MR_nullable __nullable
#define __MR_nonnull __nonnull
@tonyarnold
tonyarnold / FirstResponderRAC.m
Last active August 29, 2015 14:21
Detects when a text field's current editor is the parent window's first responder (the "view" here is an NSTableCellView subclass)
RACSignal *textFieldDidFocus = [[[[view rac_valuesForKeyPath:@keypath(view, textField.window.firstResponder) observer:view]
takeUntil:view.rac_willDeallocSignal] filter:^BOOL(id value) {
@strongify(view);
return [view.textField.currentEditor isEqualTo:value];
}]
mapReplace:view.textField];
@tonyarnold
tonyarnold / CocoaBotsDocument.swift
Created November 23, 2014 12:33
Demonstrates a bug in Xcode 6.1.1 when trying to call super within a closure.
import Cocoa
class CocoaBotsDocument : NSDocument {
override func saveToURL(url: NSURL, ofType typeName: String, forSaveOperation saveOperation: NSSaveOperationType, completionHandler: (NSError!) -> Void) {
performAsynchronousFileAccessUsingBlock() { fileAccessCompletionHandler in
// I should be able to call:
super.saveToURL(url, ofType: typeName, forSaveOperation: saveOperation) { error in }