Skip to content

Instantly share code, notes, and snippets.

View lexrus's full-sized avatar
🏠
Working from home

Lex Tang lexrus

🏠
Working from home
View GitHub Profile
@cxa
cxa / gist:bb8708a8214fe6d02365
Last active August 29, 2015 14:02
Swift example: Interacting with C APIs
// BridgeHeader.h
#import <unicode/uchar.h>
// UnicodeBlock.swift
import Foundation
class UnicodeBlock {
class var blocks: UnicodeBlock[] {
// Playground - noun: a place where people can play
import UIKit
// MARK: Utilities
func take<T>(slice: Slice<T>, num: Int) -> Slice<T> {
let n = (num < slice.count) ? num : slice.endIndex
return slice[0..<n]
}
@cxa
cxa / gist:226a07fa8aad9335c2fb
Last active August 29, 2015 14:13
Keyboard Layout Model with Swift
enum ShiftKeyType {
case None
case Once
case Always
}
enum PunctuationSwitcherType {
case More
case Numeric
}
@cxa
cxa / gist:48f1be2653fd35843d98
Created February 26, 2015 12:13
Convert an Integer to bytes
func bytes<T: IntegerType>(i: T) -> [UInt8] {
let p = UnsafeMutablePointer<T>.alloc(1)
p.memory = i
let b = unsafeBitCast(p, UnsafePointer<UInt8>.self)
let bytes = reduce(0..<sizeof(T), []) { $0 + [b[$1]] }
p.destroy()
return bytes
}
@mnbi
mnbi / convertDate.m
Created November 23, 2010 18:19
convert a RFC3339 date string into a NSDate object
#import <Foundation/Foundation.h>
// convert a RFC3399 date (& time) into a NSDate object
// NOTE: This function ignores fractions of a second in the RFC3339
// representation.
NSDate *getDateObject(NSString *rfc3339)
{
// Date and Time representation in RFC3399:
// Pattern #1: "YYYY-MM-DDTHH:MM:SSZ"
// 1
//
// NSDate+RFC3339.h
//
// Created by Atsushi Nagase on 3/7/11.
// Copyright 2011 LittleApps Inc. All rights reserved.
//
@interface NSDate (RFC3339)
@pplante
pplante / exception-mixin.py
Created April 10, 2011 00:28
template for nicer debugging of tornado exceptions
# Copyright (c) 2011 Phil Plante <unhappyrobot AT gmail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
@xslim
xslim / NSObject+AssociatedObjects.h
Created May 31, 2011 17:49
Blocks support for RestKit RKClient
#import <Foundation/Foundation.h>
@interface NSObject (AMAssociatedObjects)
- (void)associateValue:(id)value withKey:(void *)key; // Retains value.
- (id)associatedValueForKey:(void *)key;
@end
@steipete
steipete / gist:1239338
Created September 24, 2011 13:44
PSIsCrappyDevice
BOOL PSIsCrappyDevice(void) {
static BOOL isCrappyDevice = YES;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
BOOL isSimulator = NO;
BOOL isIPad2 = (PSIsIpad() && [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]);
BOOL hasRetina = [[UIScreen mainScreen] scale] > 1.f;
// enable animations on simulator
@xjones
xjones / ExampleClass.m
Created October 21, 2011 21:59 — forked from lukeredpath/ExampleClass.m
Macro for creating your "shared instance" using GCD
@implementation MySharedThing
// Using method 1
//
DEFINE_SHARED_INSTANCE_FOR_CLASS(MySharedThing)
// Using method 2
//
+ (id)sharedInstance
{