Skip to content

Instantly share code, notes, and snippets.

@boredzo
boredzo / gist:8640626
Last active April 23, 2016 03:37
Category on NSString to enumerate a string's substrings that match another string
#import <Foundation/Foundation.h>
@interface NSString (PRHEnumerateMatches)
- (NSUInteger) enumerateSubstringsMatchingString_PRH:(NSString *)otherString
options:(NSStringCompareOptions)mask
usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block;
@end
@boredzo
boredzo / gist:8610674
Created January 25, 2014 02:02
Crash-O-Matic banner layer
CATextLayer *crashOMaticLayer = [CATextLayer new];
CGColorRef color = CGColorCreateGenericRGB(1.0, 0.0, 0.0, 1.0);
crashOMaticLayer.backgroundColor = color;
CGColorRelease(color);
crashOMaticLayer.foregroundColor = CGColorGetConstantColor(kCGColorWhite);
crashOMaticLayer.string = @"Special Crash-O-Matic Ed.";
crashOMaticLayer.alignmentMode = kCAAlignmentCenter;
crashOMaticLayer.fontSize = 18.0;
//Your coordinates may vary.
NSSize size = { 300.0, 20.0 };
@boredzo
boredzo / PRHArrayByMap.h
Last active January 4, 2016 07:19
Function to create an NSArray with the results of calling a block on every object from an existing NSArray.
extern NSArray *PRHArrayByMap(NSArray *inArray, id (^block)(id));
@boredzo
boredzo / gist:8305377
Created January 7, 2014 19:32
Non-working Unicode-character-in-RTF-parsing code from http://stackoverflow.com/a/20944359/30461 .
//http://stackoverflow.com/a/20944359/30461
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *unicodedString = @"{\\rtf1\\ansi\\ansicpg1252\\cocoartf1265"
@"{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;\\f1\\fnil\\fcharset0 LucidaGrande;}"
@"{\\colortbl;\\red255\\green255\\blue255;}"
@"{\\*\\listtable{\\list\\listtemplateid1\\listhybrid{\\listlevel\\levelnfc23\\levelnfcn23\\leveljc0\\leveljcn0\\levelfollow0\\levelstartat1\\levelspace360\\levelindent0{\\*\\levelmarker \\{check\\}}{\\leveltext\\leveltemplateid1\\'01\\uc0\\u10003 ;}{\\levelnumbers;}\\fi-360\\li720\\lin720 }{\\listname ;}\\listid1}}"
@boredzo
boredzo / t9ify.py
Created December 10, 2013 21:39
Convert words from a line-separated file to T9 keypresses
#!/usr/bin/env python
import re
keys = [
'.,@1?!*#/\':;',
'abc2',
'def3',
'ghi4',
'jkl5',
'mno6',
@boredzo
boredzo / gist:7688181
Last active December 29, 2015 15:08
Test app for detecting URLs with no scheme
#import <Foundation/Foundation.h>
static void test(NSString *str);
int main(int argc, char *argv[]) {
@autoreleasepool {
test(@"mailto:[email protected]");
test(@"mailto:[email protected]?subject=Support%20Request&body=I%20NEED%20HELP%21");
test(@"callto://examplesteve");
test(@"callto:+17145551234");
@boredzo
boredzo / gist:7647290
Last active December 29, 2015 08:59 — forked from cbarrett/gist:7647270
/* Let's say you want to list all of the options to your program as an enum */
enum Option {
OptionFoo,
OptionBar
}
/* You want to write a procedure to generate a help message for your program. Here's the loopless/countless way: */
int option = OptionFoo;
@boredzo
boredzo / gist:7647085
Created November 25, 2013 19:19
Validating an enumeration on input using switch and a warning
//So you have an enumeration that counts up to some value.
enum {
A = 1,
B, C, D, E, F, G, H, I,
J
};
//You may be reading this enum from an input file. You'd like to check whether you have a valid value.
//One way would be to have a count value:
enum {
@boredzo
boredzo / PRHKeyValueCoding.m
Last active December 25, 2015 16:19
Macro to implement compile-time checking of KVC keys and key paths, but only in debug builds.
#import <Foundation/Foundation.h>
/*Usage:
*PRH_KVCKey(employer)
*PRH_KVCKeyPath(employer, employees, count)
*/
#define NDEBUG 1
//A selector that cannot be a valid KVC key, but is a known selector (i.e., the compiler won't warn that it's unknown), and is long enough that its length is probably unique.
@boredzo
boredzo / gist:6758640
Last active December 24, 2015 06:39
“Every Answer's Terrible”, a song for Stack Overflow questions with half a dozen shitty answers and not one solution among them. Reference: http://xkcd.com/1052/
Here's a bunch of sample code to drop into your app for free
I didn't bother test it, I just puked out a class hastily
I might not have even written it, I might've simply Googled it
It might not even work at all; the author might have bungled it
Explanations are for losers for whom the code alone's not good enough
The sort of folks infatuated with things like truly learning stuff
This code is what you asked about; is a sample not enough for you?
Is understanding the solution really such a thing for you?