Skip to content

Instantly share code, notes, and snippets.

@bjhomer
bjhomer / searchingAString.swift
Last active February 2, 2017 16:45
Incrementally searching through a string in Swift
import Foundation
let str = "abXcdXefXg"
let start = str.startIndex
var searchStart = start
while let xRange = str.range(of: "X", range: searchStart..<str.endIndex) {
searchStart = xRange.upperBound
@bjhomer
bjhomer / swift3.swift
Created October 4, 2016 23:06
What's wrong here?
func doThing(x: Int, y: Int, z: Int) -> Int {
return x + y + z
}
class Foo {
func doThing(x: Int) {
doThing(x: x, y: 2, z: 4) // extra argument 'y' in call
}
}
@bjhomer
bjhomer / wwdc_stats.txt
Last active June 6, 2016 19:30
WWDC redacted schedule statistics
# The following WWDC information is based on the initial redacted WWDC
# schedule posted each year. Session counts generally change a bit
# each year as the conference draws closer, but these numbers are
# close enough for a rough estimate.
#
# While session names and descriptions are redacted, the underlying
# data still includes information about which platforms they target.
# This allows us to do a bit of comparative analysis. These numbers
# are for "sessions" only -- Keynote, "Get Togethers", Labs, etc. have
# been excluded.
@bjhomer
bjhomer / instructions.md
Last active January 17, 2023 23:03
How to make the Day One command line tool automatically write to Day One 2

Using the Day One CLI with Day One 2

The dayone command line tool was originally written to work with Day One Classic, and by default writes its entries to the default Day One Classic journal directory1. If you want to use the dayone tool to write entries in to Day One 2 instead, there are a couple options.




Option 1. Use this option if you don't use Day One Classic at all.

@bjhomer
bjhomer / objc.m
Created April 25, 2016 20:45
How do you call the non-deprecated Swift method?
// ObjC
+ (id)modelOfClass:(Class)modelClass fromJSONDictionary:(NSDictionary *)JSONDictionary; // DEPRECATED
+ (id)modelOfClass:(Class)modelClass fromJSONDictionary:(NSDictionary *)JSONDictionary error:(NSError **)error;
class MyClass: NSObject {
var loggingProperty: String {
let selector = #selector(MyClass.loggingProperty) // error: instance member 'loggingProperty' cannot be used on type 'MyClass'
print("accessing", selector)
return "Hello"
}
}
@bjhomer
bjhomer / words.swift
Last active April 4, 2016 16:38
A command line tool to output words from input text, reading from stdin or from a passed file.
// Usage: echo "these are some words" | words
// Output:
// these
// are
// some
// words
import Foundation
import Swift
@bjhomer
bjhomer / 1-objc.h
Last active August 29, 2015 14:26
Block typedef broken-ness in Xcode 7 beta 5
#import <Foundation/Foundation.h>
// This is pretty simple, right?
typedef void(^SampleBlock)(NSString *str);
@bjhomer
bjhomer / files.swift
Last active August 29, 2015 14:20
Magical initializer delegation
class Top {
let x: Int
init() { x = 3; println("Top.init") }
}
class Bottom: Top {
var z: String
// This doesn't call `super.init`, but Top.init still runs. Why?
init(dummy: Bool) { z = "hi" }
@bjhomer
bjhomer / playground.swift
Last active August 29, 2015 14:18
Disambiguating methods with external parameter names
import Cocoa
func curry<A, B, C> ( f: (A, B) -> C ) -> (A -> B -> C) {
return { x in
return { y in
return f(x, y)
}
}
}