Skip to content

Instantly share code, notes, and snippets.

@bjhomer
bjhomer / currentTrack.swift
Last active March 20, 2024 02:21
Using ScriptingBridge from Swift.
#! /usr/bin/swift
import ScriptingBridge
@objc protocol iTunesTrack {
optional var name: String {get}
optional var album: String {get}
}
@objc protocol iTunesApplication {
@bjhomer
bjhomer / odd.swift
Last active August 29, 2015 14:10
Swift shorthand argument syntax oddities
// The Swift Programming Language book from Apple states the following:
//
// Swift automatically provides shorthand argument names to inline closures,
// which can be used to refer to the values of the closure’s arguments by the
// names $0, $1, $2, and so on.
// In Xcode 6.1 and 6.2b1, it appears that this is only true IF the last argument
// is referenced using shorthand syntax somewhere in the closure body. See below
// for details.
protocol FooProtocol {
func blah()
}
struct Foo : FooProtocol {
func blah() {}
}
@bjhomer
bjhomer / .gitconfig
Created March 13, 2015 18:15
A git alias to delete branches whose remote tracking branches are gone
# Drop this into your ~/.gitconfig
[alias]
delete-merged = !bash -c '\
REMOTE=$1 && \
REMOTE=${REMOTE:="origin"} && \
echo "Fetching $REMOTE" && \
git fetch $REMOTE --prune && \
git branch -vv | grep "gone]" | awk \"{ print \\$1 }\" | xargs git branch -d' -
@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)
}
}
}
@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 / 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 / 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
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 / 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;