Skip to content

Instantly share code, notes, and snippets.

View eldesperado's full-sized avatar
😁
I am busy being happy

Trung Pham eldesperado

😁
I am busy being happy
View GitHub Profile
@eldesperado
eldesperado / dispatch_after.swift
Created September 23, 2015 10:59
Top-level utility dispatch_after function
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
@eldesperado
eldesperado / StoredProperty.swift
Last active October 9, 2015 12:08
Stored Property in Swift Extension
import ObjectiveC
// Declare a global var to produce a unique address as the assoc object handle
var AssociatedObjectHandle: UInt8 = 0
extension CCNode {
var previousX: CGFloat? {
get {
return objc_getAssociatedObject(self, &AssociatedObjectHandle) as? CGFloat
}
import UIKit
import ReactiveCocoa
struct AssociationKey {
static var hidden: UInt8 = 1
static var alpha: UInt8 = 2
static var text: UInt8 = 3
}
// lazily creates a gettable associated property via the given factory
@eldesperado
eldesperado / ArrayLiteralConvertible.swift
Created November 9, 2015 03:50
Conforming types can be initialized with array literals.
<# type #> <# extendedType #> : ArrayLiteralConvertible {
required init(arrayLiteral elements: <# elementType #>...) {
self.<# storage #> = elements
}
}
@eldesperado
eldesperado / CollectionType.swift
Created November 9, 2015 03:52
Conformity to CollectionType simply requires implementing the methods of Indexable.
extension <# extendedType #> : CollectionType {
var startIndex: <# indexType : ForwardIndexType #> { return <# startIndex #> }
var endIndex: <# indexType : ForwardIndexType #> { return <# endIndex #> }
subscript (index: <# indexType : ForwardIndexType #>) -> <# elementType #> {
return <# subscript value #>
}
}
@eldesperado
eldesperado / Hashable.swift
Created November 9, 2015 03:52
Conforming types may be used in switch statements and as the element of a Set.
extension <# extendedType #> : Hashable {
var hashValue: Int { return <# hashValue #> }
}
func ==(lhs: <# extendedType #>, rhs: <# extendedType #>) -> Bool {
return <# comparator #>
}
@eldesperado
eldesperado / SequenceType.swift
Created November 9, 2015 03:53
Conforming types may be used in for-in statements.
extension <# extendedType #> : SequenceType {
func generate() -> <# generatorType #><<# elementType #>> {
<# generator state #>
return anyGenerator {
if <# terminal condition #> {
return nil
}
<# iteration logic #>
#import <Foundation/Foundation.h>
@interface KeyValueObserver : NSObject
@property (nonatomic, weak) id target;
@property (nonatomic) SEL selector;
/// Create a Key-Value Observing helper object.
#import "KeyValueObserver.h"
@interface KeyValueObserver ()
@property (nonatomic, weak) id observedObject;
@property (nonatomic, copy) NSString* keyPath;
@end
@implementation KeyValueObserver
- (id)initWithObject:(id)object keyPath:(NSString*)keyPath target:(id)target selector:(SEL)selector options:(NSKeyValueObservingOptions)options;
import Foundation
// MARK: Standard Input
/// Reads a line from standard input
///:returns: string form stdin
public func getLine() -> String {
var buf = String()
var c = getchar()
// 10 is ascii code for newline