Skip to content

Instantly share code, notes, and snippets.

//
// MultiDirectionAdjudicatingScrollView.swift
// Khan Academy
//
// Created by Andy Matuschak on 12/16/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
import UIKit.UIGestureRecognizerSubclass
@an0
an0 / MutateStructArray.swift
Last active February 28, 2016 23:43
Mutate structs in array
struct Foo {
var i: Int = 0
}
var arr = [Foo(), Foo()]
print(arr)
for var a in arr {
a.i = 1
}
@an0
an0 / OptionalTypeCastingIssues.swift
Last active February 28, 2016 23:46
Optional Type Casting Issues
class Foo {
}
class Bar: Foo {
}
//let b: Bar? = nil
let b: Bar? = Bar()
@an0
an0 / A2Z.swift
Last active February 28, 2016 23:41
A...Z
extension Character: ForwardIndexType {
public func successor() -> Character {
return Character(String(self).successor())
}
}
extension String: ForwardIndexType {
public func successor() -> String {
let scalars = self.unicodeScalars
let scalar = scalars[scalars.startIndex]
// Source: https://twitter.com/AirspeedSwift/status/618546432954474497
prefix func !<T>(t: T?) -> T {
return t ?? Array<Int8>(count: sizeof(T), repeatedValue: 0).withUnsafeBufferPointer {
UnsafePointer($0.baseAddress).memory
}
}
prefix func !<T: ExtensibleCollectionType>(t: T?) -> T {
return t ?? T()
@an0
an0 / PowerOfDefaultAssociatedType.swift
Last active August 29, 2015 14:25
Power of Default Associated Type
// Simplify code of http://martiancraft.com/blog/2015/07/objective-c-swift-core-data/ by using default associated type to eliminate the need of defining associated type in every subclass.
class ManagedObject {}
class Person: ManagedObject {}
class Thing: ManagedObject {}
let db: [ManagedObject] = [Person(), Thing()]
@an0
an0 / ArraysBuildingWithReduceOptimization.md
Last active August 29, 2015 14:26
Is Arrays Building with Reduce Optimizable?
@an0
an0 / CaseLetWhere.swift
Created August 6, 2015 20:15
Case-condition is Powerful
for case (let x, 0) in [(0, 0), (0, 1), (1, 0), (1, 1)] where x > 0 {
print(x)
}
@an0
an0 / GenericTypesNotThatWorseThanProtocols.swift
Created August 7, 2015 14:47
Generic Types are Not That Worse than Protocols
// Response to http://www.russbishop.net/swift-associated-types:
// 1. Type parameter leaking is a real problem(the `S` parameter of `buyFoodAndFeed` below can not be omitted — "We have no way to express that we don't care about the Supplement type parameter") but not as severe as is described in the article because non-generic subclasses of generic superclasses do not need type parameter re-specification when being used.
// 2. It is not true that "we have no way to link the type of F to Food". We can link Animal.F to Store.G. See `buyFoodAndFeed` below.
class Food {
required init() {
}
}
@an0
an0 / ImplicitlyUnwrappedOptionalUnwrappedUnexpected.swift
Last active August 29, 2015 14:26
Implicitly Unwrapped Optional is Really Dangerous and Its Type Matching is Buggy
/* Implicitly unwrapped optional is really dangerous, especially when unwrapped unexpected.
For example, I expect here `a` will be checked again `nil` first before being unwrapped and compared to `b`.
That is to say, I thought there are `==(lhs: T?, rhs: T)` and `==(lhs: T, rhs: T?)`. But there aren't.
There are only `==(lhs: T, rhs: T)` and `==(lhs: T?, rhs: T?)`.
Reasonably, (lhs: T!, rhs: T) matches better the former than latter so the former is picked so crash. */
let a: Int! = nil
let b: Int = 1