Skip to content

Instantly share code, notes, and snippets.

Nick Lockwood nicklockwood

Block or report user

Report or block nicklockwood

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Withable.swift
/// Withable is a simple protocol to make constructing
/// and modifying objects with multiple properties
/// more pleasant (functional, chainable, point-free)
public protocol Withable {
init()
}
public extension Withable {
/// Construct a new instance, setting an arbitrary subset of properties
init(with config: (inout Self) -> Void) {
View gist:35bc1734f2f8c97177010b1188345921
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) ->
guard let obj = (self.representedObject as? ModelType)?.collection(at:row) else {
return nil
}
switch tableColumn?.identifier.rawValue ?? "" {
case "column1":
return obj.field1
case "column2":
return objc.field2
//...
@nicklockwood
nicklockwood / StringsTest.swift
Last active Jan 16, 2019
string concat benchmarks
View StringsTest.swift
import XCTest
let count = 50000
class StringsTestTests: XCTestCase {
// MARK: Copying
func testInterpolation() {
var foo = ""
@nicklockwood
nicklockwood / main.swift
Created Dec 1, 2017
Thread-safe cache implementation benchmark
View main.swift
var cache = [Int: Int]()
let queue = DispatchQueue(label: "cacheQueue", attributes: .concurrent)
let iterations = 100000
// In the first run, cache is empty so we're writing each time
do {
let start = CFAbsoluteTimeGetCurrent()
for i in 0 ... iterations {
var exists = false
queue.sync {
@nicklockwood
nicklockwood / UnicodeScalarView.swift
Last active Sep 21, 2017
UnicodeScalarView.swift
View UnicodeScalarView.swift
// This is a really simple drop-in replacement for String.UnicodeScalarView
// As of Swift 3.2, String.UnicodeScalarView no longer supports slice operations, and
// String.UnicodeScalarView.Subsequence is ~5x slower
//
// Only a small subset of methods are implemented, specifically the ones useful for
// implementing a parser or lexer that consumes a string by repeatedly calling popFirst()
//
// I've benchmarked popFirst() as ~7x faster than String.UnicodeScalarView.Subsequence in Swift 3.2 and 4.0
// The performance is close to that of String.UnicodeScalarView in Swift 3.1, but may be slightly worse in some use cases
View Pattern for Swift Errors.swift
// Swift's untyped errors are a goddam PiTA. Here's the pattern I use to try to work around this.
// The goal is basically to try to guarantee that every throwing function in the app throws an
// ApplicationError instead of some unknown error type. We can't actually enforce this statically
// But by following this convention we can simplify error handling
enum ApplicationError: Error, CustomStringConvertible {
// These are application-specific errors that may need special treatment
case specificError1
case specificError2(SomeType)
@nicklockwood
nicklockwood / error-propagation.swift
Last active Aug 17, 2017
Typed error use-case example
View error-propagation.swift
enum ValueError: Error {
case notFound
case typeMismatch
}
struct PropertyError: Error {
let name: String
let error: ValueError
}
View json.md

A very basic JSON parser, written in [new language]. Only supports String, Int and Array so far.

json := "[42, \"hello\", [\"good\", \"bye\"]]"

JSON = String|Int|[JSON] // TODO: true, false, null, object

skipSpace(json: String) String {
    input := json
    while input.first == " " {
@nicklockwood
nicklockwood / wishlist.md
Last active Jun 9, 2016
App Store Wishlist
View wishlist.md

App Store Wishlist

Some changes (besides the subscriptions features added recently) that I'd like to see in the App Store.

NOTE: This is in no way an attempt at an exhaustive list of all things wrong with the App Store. The focus of this list is mostly around improvements to make it easier for developers to make money from their apps without annoying customers. Other problems such as the broken review process, or inability to contact customers directly, etc. are out of scope.

  • Time or feature-limited demos with IAP to get full version. You could argue that this is already possible, but time-limits are against App Store policy, and apps that do it appear in the "Free" section with "Offers IAP" messaging, making them indistinguishable from pay-to-win junk. These would appear under "Paid" (and/or in their own category) and would have clear messaging that this was a free trial of a paid app. There would be other benefits too: You'd be able to gift a copy of the full app to someone witho
@nicklockwood
nicklockwood / gist:493da04de6ea5cbc7a7e
Last active Dec 28, 2016
Animating layer contents when bounds changes
View gist:493da04de6ea5cbc7a7e
@interface AnimatedContentsLayer: CALayer
@property (nonatomic, assign) CGSize animatedSize;
@end
@implementation AnimatedContentsLayer
@dynamic animatedSize;
You can’t perform that action at this time.