Skip to content

Instantly share code, notes, and snippets.

@bjhomer
bjhomer / gist:8af670a31944b54bdfef
Last active August 29, 2015 14:00
Tokenizer comparison
Input: @"This is a puppy that is happy. 🐶❄️ = 😀🐶. 你好,我喜欢汉字。"
kCFStringTokenizerUnitWordBoundary results:
token:[This]
token:[ ]
token:[is]
token:[ ]
token:[a]
token:[ ]
token:[puppy]
@bjhomer
bjhomer / gist:b633149338ed4114d2a8
Last active August 29, 2015 14:00
Requirements for a good UIAlertView+Blocks implementation
  1. Must have a method similar to -addButtonWithTitle:handler:
  2. Must still work if some buttons are added using the old -addButtonWithTitle: API.
  3. Must still allow setting a delegate, and the delegate must work.

Note that #2 means that handler blocks must not simply be added to an array and located by button index, as there may be more buttons than there are handlers. This is a common error in popular implementations.

@bjhomer
bjhomer / pprofile.py
Created June 25, 2014 16:39
A simple python script to more easily inspect provisioning profiles.
#! /usr/bin/python
import sys
import os
def main():
if len(sys.argv) < 2:
print "Usage: profile.py <path> | <UUID>"
sys.exit()
// Does this code create 10000 live instances of MyObj before returning?
// Or will each one be released on the following iteration?
void foo() {
NSLog(@"Starting!");
int i=0;
start:
id obj = [MyObj new];
if (++i<10000) {
goto start;
@bjhomer
bjhomer / fish_prompt.fish
Last active August 29, 2015 14:03
My fish prompt
function fish_prompt --description 'Write out the prompt'
# Just calculate these once, to save a few cycles when displaying the prompt
if not set -q __fish_prompt_normal
set -g __fish_prompt_normal (set_color normal)
end
set -l delim '$'
switch $USER
//
// DOXLabelWrapper.m
// Autolayout
//
// Created by BJ Homer on 7/11/14.
// Copyright (c) 2014 BJ Homer. All rights reserved.
//
#import "DOXLabelWrapper.h"
@bjhomer
bjhomer / keyDown.m
Last active August 29, 2015 14:04
Looking for the best way to use switch based on a single character. Seems like I have to convert everything to Int(), which can be very verbose.
-(void)keyDown:(NSEvent *)event
{
unichar ch = [[event charactersIgnoringModifiers] characterAtIndex:0];
if (ch == NSUpArrowFunctionKey && (event.modifierFlags & NSCommandKeyMask)) {
// Scroll to top
return;
}
else if (ch == NSDownArrowFunctionKey && (event.modifierFlags & NSCommandKeyMask)) {
// Scroll to bottom
@bjhomer
bjhomer / gist:b0345c44dfbdb8cb7398
Last active August 29, 2015 14:04
Nil-fallback operator suggestions
// Looking for a Swift replacement for the following ObjC code:
// id x = foo ?: bar
//
// Which of the following do you like, if any? Any other suggestions?
// (Note that '?' cannot be used in a custom operator.)
x = foo !|| bar
x = foo |< bar
x = foo ||< bar
@bjhomer
bjhomer / betterMyImageView.m
Last active August 29, 2015 14:05
NSImageView gobbling dragging methods
@implementation MyImageView
- (void)registerForDraggedTypes:(NSArray *)newTypes {
if (self.editable) {
[super registerForDraggedTypes:newTypes];
}
}
- (void)setEditable:(BOOL)editable
{
@bjhomer
bjhomer / broken.swift
Last active August 29, 2015 14:05
This does not compile.
class Foo {
var closure: () -> () = {}
init() {}
func nothing() {}
func setupClosure() {
closure = {
[weak self] in
self?.nothing() // <-- this returns a "Void?", which means that the rhs's