Skip to content

Instantly share code, notes, and snippets.

View josephlord's full-sized avatar

Joseph Lord josephlord

View GitHub Profile
import UIKit
class GenericTVCDelegate<A> : NSObject, UITableViewDataSource {
var cellCreator:A->UITableViewCell
var data:[A]
init(cellCreator:A->UITableViewCell, data:[A]) {
self.cellCreator = cellCreator
self.data = data
}
//}
@josephlord
josephlord / WrappedClasses.swift
Created September 9, 2015 22:07
As presented at iOSDevUK briefly during the Swift Perfromance talk. See blog.human-friendly.com for details. This is exploratory and example code, not yet performance tested. I plan to blog more about the array type later.
final class Bar {
var i = 0
}
struct Foo {
var i:Int {
get {
return bar.i
}
set {
@josephlord
josephlord / DictionaryProtocol.swift
Last active August 29, 2015 14:26
Extending [String:T] - Compiles but fails at linking radar raised rdar://22028034
public protocol StringyType {
var string:String { get }
init(_ string:String)
}
extension String : StringyType {
public var string:String{ return self }
public init(_ string:String) {
self = string
}
@josephlord
josephlord / testingAssertions.swift
Created July 15, 2015 21:46
Demonstration of assertion behaviour in different build modes
import Foundation
func testAssert(x:Int)->Int {
var res = 0
if x > 0 {
res += 3
assertionFailure("testAssert")
res += 5
return res
@josephlord
josephlord / parseStarshipsRefactor.swift
Last active August 29, 2015 14:16
Extracted inner object parsing JSON example for Star Wars API - http://swapi.co/api/starships/?format=json
import Foundation
/// http://swapi.co/api/starships/?format=json With "'s escaped.
let starshipsJSON = "{\"count\":36,\"next\":\"http://swapi.co/api/starships/?format=json&page=2\",\"previous\":null,\"results\":[{\"name\":\"Sentinel-class landing craft\",\"model\":\"Sentinel-class landing craft\",\"manufacturer\":\"Sienar Fleet Systems, Cyngus Spaceworks\",\"cost_in_credits\":\"240000\",\"length\":\"38\",\"max_atmosphering_speed\":\"1000\",\"crew\":\"5\",\"passengers\":\"75\",\"cargo_capacity\":\"180000\",\"consumables\":\"1 month\",\"hyperdrive_rating\":\"1.0\",\"MGLT\":\"70\",\"starship_class\":\"landing craft\",\"pilots\":[],\"films\":[\"http://swapi.co/api/films/1/\"],\"created\":\"2014-12-10T15:48:00.586000Z\",\"edited\":\"2014-12-22T17:35:44.431407Z\",\"url\":\"http://swapi.co/api/starships/5/\"},{\"name\":\"Death Star\",\"model\":\"DS-1 Orbital Battle Station\",\"manufacturer\":\"Imperial Department of Military Research, Sienar Fleet Systems\",\"cost_in_credits\":\"1000000000000\",\"length\":\"120000\
@josephlord
josephlord / starshipparseExample.swift
Created March 2, 2015 01:34
Swift parsing example without using any 3rd party libraries, just the Cocoa NSJSONSerialization.JSONObjectWithData.
import Foundation
/// http://swapi.co/api/starships/?format=json With "'s escaped.
let starshipsJSON = "{\"count\":36,\"next\":\"http://swapi.co/api/starships/?format=json&page=2\",\"previous\":null,\"results\":[{\"name\":\"Sentinel-class landing craft\",\"model\":\"Sentinel-class landing craft\",\"manufacturer\":\"Sienar Fleet Systems, Cyngus Spaceworks\",\"cost_in_credits\":\"240000\",\"length\":\"38\",\"max_atmosphering_speed\":\"1000\",\"crew\":\"5\",\"passengers\":\"75\",\"cargo_capacity\":\"180000\",\"consumables\":\"1 month\",\"hyperdrive_rating\":\"1.0\",\"MGLT\":\"70\",\"starship_class\":\"landing craft\",\"pilots\":[],\"films\":[\"http://swapi.co/api/films/1/\"],\"created\":\"2014-12-10T15:48:00.586000Z\",\"edited\":\"2014-12-22T17:35:44.431407Z\",\"url\":\"http://swapi.co/api/starships/5/\"},{\"name\":\"Death Star\",\"model\":\"DS-1 Orbital Battle Station\",\"manufacturer\":\"Imperial Department of Military Research, Sienar Fleet Systems\",\"cost_in_credits\":\"1000000000000\",\"length\":\"120000\
@josephlord
josephlord / NSJSONSerializationWrapper.swift
Created March 1, 2015 17:04
A very minimal wrapper round NSJSONSerialization.JSONObjectWithData returning a simple
enum JSONParseResult {
case Success(AnyObject)
case Error(NSError)
}
/// Parses UTF-8 data to the appropriate object.
func parseJSON(data:NSData)->(JSONParseResult) {
var err:NSError?
let parsed:AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &err)
if let err = err {
@josephlord
josephlord / json_example.swift
Last active August 29, 2015 14:16
Takes the LumaJSON parsing example and shows that it works directly on NSJSONSerialization result
import Foundation
var str = "Hello, playground"
let jsonStr = "{\"user\": {\"name\": \"jquave\",\"id\": 542,\"url\": \"http://jamesonquave.com\"},\"friend_ids\": [299,341,492],\"alert_message\": \"Please verify e-mail address to continue\"}"
var err:NSError?
let data = jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) ?? NSData()
typealias JSONDict = [String:AnyObject]
@josephlord
josephlord / swift_1_2_source_changes.swift
Created February 18, 2015 00:06
Swift 1.2 beta required code changes and bugs
// Will be fixed
let sub:SubClass = s as? SubClass ?? SubClass(2)
// Use this for Beta 1
let sub:SubClass = (s as? SubClass) ?? SubClass(2)
// Setup
let dict:[String:Int]? = ["a":1, "b":2]
// Swift 1.1 - Removed
let x:Int? = dict.map { $0["a"] }?
// Swift 1.2
@josephlord
josephlord / swift_1_2.swift
Created February 18, 2015 00:04
As much 1.2 only Swift as you can fit on a slide
class Swift_1_2 {
static var v = UIView()
static let d = [ "a" : 1, "b" : 3 ]
static func bar() {
let x: Set<Int>
if let a = d["a"], b = d["b"] where a < b {
x = [a, b, b-a]
} else {
x = []
}