Skip to content

Instantly share code, notes, and snippets.

// 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 / movewindow.m
Last active July 14, 2021 18:51
Why not -[NSView mouseDownCanMoveWindow]?
// You might think that a view could trigger window movement by overriding -[NSView mouseDownCanMoveWindow]
@interface DraggyView : NSView
@end
@implementation DraggyView
- (BOOL)mouseDownCanMoveWindow {
return YES;
}
@end
@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
@bjhomer
bjhomer / 1 badDiff.diff
Last active August 29, 2015 14:05
Semantically poor lines shouldn't match
context
context
-AAAA
-AAAA
-AAAA
+BBBB
+BBBB
+BBBB
+BBBB
{
@bjhomer
bjhomer / gist:cc3f3155d3b109f61601
Created October 31, 2014 17:06
LLDB and Swift Arrays are not friends.
// What I would expect:
(lldb) p newPaths
([AnyObject]) $R1 = 2 values {
[0] = "This is a string",
[1] = "This is another string"
}