Skip to content

Instantly share code, notes, and snippets.

View isoiphone's full-sized avatar

Jacob Schwartz isoiphone

View GitHub Profile
// oh dear god...
if ([[[[_actionSheet superview] superview] nextResponder] respondsToSelector:@selector(setPassthroughViews:)]) {
[[[[_actionSheet superview] superview] nextResponder] performSelector:@selector(setPassthroughViews:) withObject:nil];
}
@isoiphone
isoiphone / arrayWithoutDuplicates.m
Last active August 29, 2015 13:57
Given an array, remove the duplicates and return a unique array keeping the first occurrence of the duplicates and the order. [@2, @1, @3, @1, @2] --> [@2, @1, @3]
#import <Foundation/Foundation.h>
#import <stdio.h>
@interface Test : NSObject
@end
@implementation Test
+ (NSArray*)arrayWithoutDuplicates:(NSArray*)a {
NSMutableSet* s = [NSMutableSet set];
#import <Foundation/Foundation.h>
#import <stdio.h>
@class Node;
@interface Node : NSObject
@property (strong) Node* next;
@property (assign) NSString* val;
@end
#include "Platform.h"
bool Platform::mapFile(const char* filename, uint8_t** bytes, long* length) {
FILE* fin = fopen(Platform::pathForFile(filename).c_str(), "rb");
if (!fin) {
dbgLog("error opening file '%s'", filename);
return false;
}
@isoiphone
isoiphone / NQueens.swift
Last active August 29, 2015 14:14
N queens problem in Swift
// jacob schwartz | @isoiphone
// solves N queens problem in swift
// for N=8 there should be 92 possible solutions
// each solution is a list of numbers, these indicate the row to place queen for given column
// for example 0,4,7,5,2,6,1,3 maps to:
// Q-------
// ------Q-
// ----Q---
// -------Q
// -Q------
@isoiphone
isoiphone / main.c
Last active August 29, 2015 14:14
one line string reverse in place, because
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char *s) {
for (char *front = s, *back=s+strlen(s)-1; front < back; *front ^= *back, *back ^= *front, *front ^= *back, ++front, --back) {}
}
void test(const char* s) {
char* tmp = strdup(s);
@isoiphone
isoiphone / gist:a1c8ec2e8fceae3be56b
Last active August 29, 2015 14:15
Swift 1.2 breaks my __FUNCTION__
// no longer compiles in swift 1.2
func someFunc(block: () -> (), functionName: String = __FUNCTION__) {
println("NEAT!")
block()
}
someFunc {
println("oh")
}
@isoiphone
isoiphone / gist:4dc4cb6531b31998e8a7
Created March 11, 2015 22:12
handling realm.io errors on startup (lazy way to handle migration changes, missing encryption key, etc)
// this is the first call to defaultRealm() (ie: it lives in application:didFinishLaunchingWithOptions)
// see: SwiftTryCatch https://github.com/williamFalcon/SwiftTryCatch for a wrapper around ObjectiveC exceptions.
SwiftTryCatch.try({
RLMRealm.defaultRealm()
}, catch: { exception in
log.error("realm exception: \(exception.description). Will remove local database.")
NSFileManager.defaultManager().removeItemAtPath(RLMRealm.defaultRealmPath()!, error: nil)
@isoiphone
isoiphone / gist:fd907d9c2e02838200f1
Created March 12, 2015 00:30
Manual thread storage, may stick with this pattern.
extension RLMRealm {
static var threadInstance: RLMRealm! {
let threadDictionary = NSThread.currentThread().threadDictionary
if let inst = threadDictionary["SharedRealmInst"] as! RLMRealm? {
return inst
} else {
let inst = defaultRealmWithErrorHandling()
threadDictionary["SharedRealmInst"] = inst
return inst
}
var qp = 0.0
for var n=0.0; ;++n {
qp += (1-(n%2)*2)/(2*n+1)
println("\(qp*4)")
}