Skip to content

Instantly share code, notes, and snippets.

@uliwitness
uliwitness / NSStringCanDoNullsButNSLogCant.m
Created August 28, 2015 14:49
Test program that shows that NSString can do '\0' characters, however NSLog will terminate its entire output at encountering the first one:
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *str = [[NSString alloc] initWithBytes: "\0\r" length: 2 encoding: NSASCIIStringEncoding];
NSLog(@"len = %zu \"%@\"", str.length, str);
}
}
@uliwitness
uliwitness / gist:518cf136e2f4cf5dd855
Created April 30, 2015 10:46
Higher-level ARC return value workaround
@protocol TellARCAboutVoidReturn
-(void) performSelector: (SEL)inAction withObject: (id)inSender;
@end
...
[(id<TellARCAboutVoidReturn>)receiver performSelector: @selector(someVoidAction:) withObject: theSender];
@uliwitness
uliwitness / gist:6c065f90362e83b293f3
Last active August 29, 2015 14:21
Most iOS apps have a static main table showing the different sections of the application. Sometimes you have different variants of the same app (e.g. Lite and full, or branded versions) that use the same sources. Would be nice to just be able to #ifdef out sections one version doesn't need. This is an attempt at Swiftifying the ObjC pattern I us…
//
// ViewController.swift
// SwiftTableTestIOS
//
import UIKit
class ViewController: UITableViewController {
// These enum cases should be in the order the user should see the items in:
@uliwitness
uliwitness / components_separated_by_string.cpp
Created August 10, 2015 14:39
I'm a bit surprised C++ doesn't have an equivalent to Objective C's componentsSeparatedByString, but it's easy enough to roll your own, I suppose.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> components_separated_by_string( const string& s, const string& delimiter )
{
if( s.size() == 0 )
return vector<string>();
@uliwitness
uliwitness / blockreturningfunc.c
Last active August 29, 2015 14:27
The horrible syntax you need to declare a function that returns a block:
#include <stdio.h>
void (^blockreturner(void))( int a, int b )
{
return ^(int a, int b){ printf("%d %d\n",a,b); };
}
int main(int argc, char *argv[]) {
blockreturner()( 1, 2 );
@uliwitness
uliwitness / forwarding_for_default_protocol_implementations.m
Created September 7, 2015 02:30
It's annoying that Objective-C doesn't let you provide a default implementation for a protocol. Maybe one could just create a proxy with that?
//
// main.m
// ProtocolImplementations
//
// Created by Uli Kusterer on 07/09/15.
// Copyright (c) 2015 Uli Kusterer. All rights reserved.
//
#import <Foundation/Foundation.h>
@uliwitness
uliwitness / constructorless_works.cpp
Last active September 8, 2015 16:42 — forked from anonymous/int_doesnt_work.cpp
The second example's second case won't compile (complains it expected a class after ~) but the third one through the template does. Anyone know what I'm doing wrong?
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <malloc/malloc.h>
class JollyRoger
{
public:
@uliwitness
uliwitness / gist:2343351
Created April 9, 2012 13:21
Shell script to extract a folder from a git repository to make a new repository with only the history for that folder.
# param 1 is path to root of repository to clone
# param 2 is folder to clone to. The folder's name must be the
# same as the folder to be extracted from the repository at param 1.
GIT='/Applications/Xcode.app/Contents/Developer/usr/bin/git'
$GIT clone --no-hardlinks "$1" "$2"
cd "$2"
$GIT filter-branch --subdirectory-filter "`basename $2`" HEAD #-- --all
@uliwitness
uliwitness / gist:2415382
Created April 18, 2012 17:47
An (untested) macro and category for easily creating new index sets from unchanging content.
@interface NSIndexSet (ULIIndexSetCreation)
+(NSIndexSet*) indexSetWithIndexes: (const NSInteger [])indexes count: (NSUInteger)count;
@end
@implementation NSIndexSet (ULIIndexSetCreation)
+(NSIndexSet*) indexSetWithIndexes: (const NSInteger [])indexes count: (NSUInteger)count
@uliwitness
uliwitness / gist:4045701
Created November 9, 2012 13:38
Sample script that shows the warning for which I want to find the 'foo' to pass to -Werror=foo (same as you'd pass to -Wfoo)
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
@end
@implementation MyClass
@end