Skip to content

Instantly share code, notes, and snippets.

@uliwitness
uliwitness / cardflipper.hc
Last active November 21, 2015 15:33
How to flip through cards in HyperCard.
-- first approach:
-- sadly this causes a deep nested hierarchy where "go" calls "openCard"
-- which calls "go", eventually running out of memory.
on openCard
wait 5 seconds
go next
end openCard
-- second approach:
-- idle gets called periodically by HyperCard, but you don't have control over
@uliwitness
uliwitness / cppwrapper.cpp
Created October 21, 2015 00:16
Wrapping C++ in Swift via a C wrapper.
#include "cppwrapper.h"
#include "buff.hpp"
extern "C" void buff_deinit( buff* _this )
{
delete _this;
}
extern "C" buff* buff_init( int type, double amount, double max_amount, double start_angle, double relative_angle, double max_distance, double bleedthrough, bool permanent )
{
@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 / 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 / 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 / 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 / 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>();
#import <Foundation/Foundation.h>
#include <iostream>
@interface ULIFoo : NSObject
@end
@implementation ULIFoo
-(id) init
@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 / 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];