Skip to content

Instantly share code, notes, and snippets.

@khanlou
khanlou / SKValueObject.h
Last active Aug 29, 2015
SKValueObject
View SKValueObject.h
//
// SKValueObject.h
// TinyType
//
// Created by Soroush Khanlou on 5/15/14.
// Copyright (c) 2014 Soroush Khanlou. All rights reserved.
//
#import <Foundation/Foundation.h>
View NSObject+IfClass.h
#import <Foundation/Foundation.h>
@interface NSObject (IfClass)
- (id)ifClass:(Class)aClass;
@end
View UIColor+LiteralConvertible.swift
import UIKit
extension UIColor: IntegerLiteralConvertible {
class func convertFromIntegerLiteral(value: IntegerLiteralType) -> UIColor {
let r = Double((value >> 16) & 0xFF) / 255;
let g = Double((value >> 8) & 0xFF) / 255;
let b = Double((value) & 0xFF) / 255;
return UIColor(red:r, green:g, blue:b, alpha:1.0)
}
View MappedArray.m
@interface MappedArray ()
@property (nonatomic) NSArray *backingArray;
@end
@implementation MappedArray
- (instancetype)initWithArray:(NSArray *)array transformationBlock:(id (^)(id object))block {
self = [super init];
@khanlou
khanlou / gist:6091415
Created Jul 26, 2013
Blocks are created on the stack frame that they are on
View gist:6091415
void (^block)();
if (condition) {
block = ^{
NSLog(@"some code");
};
} else {
block = ^{
NSLog(@"other code");
};
}
@khanlou
khanlou / download_all_cloud_app_files.py
Created Sep 2, 2013
This is a simple python script for downloading all of the files in your cloud app. Caveats: "bookmarks" can't be downloaded, so they'll be outputted in the log. All files will be downloaded into the same directory as the python script. It requires pycloudapp, which you can find here: https://github.com/originell/pycloudapp.
View download_all_cloud_app_files.py
# requires https://github.com/originell/pycloudapp
from cloudapp.cloud import Cloud
import urllib
mycloud = Cloud()
mycloud.auth("username", "password")
@khanlou
khanlou / gist:7156174
Created Oct 25, 2013
Scottish Wizard Voodoo Drink
View gist:7156174
View BugWithDefaultParam.swift
func doThing(defaultable value: Int = 4, _ required: Void -> Int) -> Int {
return value + required()
}
doThing(defaultable: 4, { 6 }) // works
doThing { 6 } // works
doThing({ 6 }) // doesn't compile: Missing argument for parameter #2 in call
@khanlou
khanlou / Times.swift
Created Nov 6, 2016
Ruby's n.times in Swift
View Times.swift
extension SignedInteger {
func times() -> AnySequence<()> {
return AnySequence<()>({ () -> AnyIterator<()> in
var count: Self = 0
return AnyIterator<()>({
if count == self {
return nil
}
View Data+HexString.swift
import Foundation
extension Data {
var hexString1: String {
return self.map({ return String(format: "%02hhx", $0) }).joined()
}
var hexString2: String {
return self.reduce("", { return $0 + String(format: "%02hhx", $1) })