Skip to content

Instantly share code, notes, and snippets.

@janodev
janodev / NSTimer+(BlockSupport).m
Last active December 17, 2015 07:48
NSTimer with safe release. Credits to Effective Objective-C 2.0.
#import <Foundation/Foundation.h>
@interface NSTimer (BlockSupport)
+ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeats;
@end
@janodev
janodev / Person.h
Last active December 17, 2015 17:59
Different ways to execute a block property. This runs in CodeRunner. Replace one of the #ifdef X below with #ifdef YES, and comment person = nil;.
#import <Foundation/Foundation.h>
#undef X
typedef void (^salute_t)();
@interface Person : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) salute_t salute;
@janodev
janodev / factorial.m
Created May 25, 2013 20:12
Factorial with recursive blocks.
#import <Foundation/Foundation.h>
// clang -framework Foundation main.m -o main && ./main
int main(int argc, char *argv[]) {
@autoreleasepool {
NSUInteger (^factorial)(NSUInteger n);
NSUInteger (^__block __weak weakFactorial)(NSUInteger n);
weakFactorial = factorial = ^ NSUInteger (NSUInteger n) {
return n==0 ? 1 : n * weakFactorial(n-1);
@janodev
janodev / Person.m
Created June 27, 2013 11:48
Dynamic get/set. Watch it, I'm using [[NSThread currentThread] threadDictionary] for thread-safe access.
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
@interface Person : NSObject
@end
@implementation Person
@end
@interface Person(dynamicProperties)
@janodev
janodev / ARSketchView.h
Last active December 22, 2015 19:19
ARSketchView
@interface ARSketchView : UIView
- (IBAction) clear;
- (IBAction) replay;
@end
@janodev
janodev / gist:6620842
Last active December 23, 2015 10:19
“pod install” setting 32 bits instead 64? update CocoaPods to the xcode5-support branch
sudo gem install bundler
printf "gem 'cocoapods', :git => 'http://github.com/CocoaPods/CocoaPods', :branch => 'xcode-5-support' \n" > Gemfile
printf "gem 'xcodeproj', :git => 'http://github.com/CocoaPods/Xcodeproj', :branch => 'redacted-support' \n" >> Gemfile
sudo bundle install
sudo bundle exec pod install

Generate the list yourself:

$ cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS*.sdk/System/Library/Frameworks/UIKit.framework/Headers
$ grep -H UI_APPEARANCE_SELECTOR ./* | sed 's/ __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;//'

UIActivityIndicatorView

# Install Xcode 5.0.1 GM seed, open it, install Command Line Tools.
# Uninstall macports, see https://www.macports.org/guide/chunked/installing.macports.uninstalling.html
# Run the following:
curl -O https://distfiles.macports.org/MacPorts/MacPorts-2.2.0.tar.bz2
tar jxvf MacPorts-2.2.0.tar.bz2
cd MacPorts-2.2.0
CC=/usr/bin/cc ./configure --prefix=/opt/local --with-install-user=root --with-install-group=admin --with-directory-mode=0755 --enable-readline \
--with-tclpackage=/Library/Tcl \
--with-tcl=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Tcl.framework \
@janodev
janodev / MJGAvailability.h
Last active December 29, 2015 06:39
Emit build warnings in Xcode 5 for methods only available on a later iOS version than the deployment target. The pch below is set to warn on methods newer than 6.1. Change the __IPHONE_6_1 part for a different version. Code taken from http://stackoverflow.com/a/8919108/412916 and http://stackoverflow.com/a/19704587/412916
//
// MJGImageLoader.h
// MJGFoundation
//
// Created by Matt Galloway on 18/01/2012.
// Copyright 2012 Matt Galloway. All rights reserved.
//
/**
* Example usage:
@janodev
janodev / BigFactorial.java
Created May 1, 2014 10:53
Factorial with tail-call optimization in Java 8
// Code from “Functional Programming in Java, Chapter 7”.
import java.util.function.Function;
import java.util.stream.Stream;
import java.math.BigInteger;
@FunctionalInterface
interface TailCall<T> {