Skip to content

Instantly share code, notes, and snippets.

@mpurland
mpurland / SwiftCompilerInfiniteLoopTest.swift
Last active December 28, 2015 00:19
Example to cause the Swift compiler to go into an infinite loop
import Foundation
typealias Mask = UInt64
enum Category: UInt8 {
case A
case B
case C
case D
case E
@mpurland
mpurland / OptionalEquatable.swift
Created November 13, 2015 00:23
Swift classes extending NSObject overriding ==
// The following shows that Equatable for NSObject is not necessarily safe
// for Swift classes that extend NSObject and override Equatable.
// Reference: http://natashatherobot.com/swift-equatable-with-optionals/
class TitleNSObject: NSObject {
var title: String = ""
init(title: String) {
super.init()
self.title = title
@mpurland
mpurland / objcpropertychangemacro.m
Last active August 29, 2015 14:13
Objective-C property change macro for better KVO using willChangeValueForKey/didChangeValueForKey
// Note: This requires libextobjc: https://github.com/jspahrsummers/libextobjc
inline static void propertyChangeObject(NSObject *object, NSString *keypath, void (^block)()) {
[object willChangeValueForKey: keypath];
block();
[object didChangeValueForKey: keypath];
}
#define propertyChange(__KEY, block) propertyChangeObject(self, @keypath(self.__KEY), block)
#define propertyChange2(keypath, block) propertyChangeObject(self, keypath, block)
@mpurland
mpurland / gist:c704e67f9ceb2e00260b
Created November 13, 2014 07:41
Swift: Generic method for enumerating all values of an enum of type Int
enum Reindeer: Int {
case Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donner, Blitzen, Rudolph
static var allValues: [Reindeer] {
return allValuesGenerator({ Reindeer(rawValue: $0) })
}
}
func allValuesGenerator<T>(fromValue: Int -> T?) -> [T] {
return Array(
@mpurland
mpurland / contents.xcplayground
Created June 12, 2014 20:21
Swift playground multiple files or renamed files
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='macosx'>
<sections>
<code source-file-name='section-1.swift'/>
<code source-file-name='section-2.swift'/>
<code source-file-name='custom-filename.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>