Skip to content

Instantly share code, notes, and snippets.

View Morse-Code's full-sized avatar

Christopher Morse Morse-Code

View GitHub Profile
@Morse-Code
Morse-Code / directoryListing.m
Created April 6, 2013 21:53
List contents of directory and all sub-directories.
NSFileManager*fileMgr;
NSString*entry;
NSString*documentsDir;
NSDirectoryEnumerator*enumerator;
BOOL isDirectory;
// Create file manager
fileMgr =[NSFileManager defaultManager];
// Path to documents directory
documentsDir =[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// Change to Documents directory[fileMgr changeCurrentDirectoryPath:documentsDir];
@Morse-Code
Morse-Code / ternary.m
Created April 6, 2013 20:41
Objective-C Ternary Trick One unique ability of using the ternary operand with objects is that the code can be shorted even further – if the value being checked is also the value after the ternary operator, the value can be omitted from the operation:
- (void)displayErrorMessage:(NSString *)msg
{
// Even shorter version, when the operant is the same as the "True" case:
NSString *msgFullString = [NSString stringWithFormat:@"Message: %@", msg ? : @"Unknown error"];
NSLog(@"%@", msgFullString);
}
@Morse-Code
Morse-Code / ternary.m
Created April 6, 2013 20:38
Objective-C Ternary Trick One unique ability of using the ternary operand with objects is that the code can be shorted even further – if the value being checked is also the value after the ternary operator, the value can be omitted from the operation:
- (void)displayErrorMessage:(NSString *)msg
{
// Even shorter version, when the operand is also the "True" case:
NSString *msgFullString = [NSString stringWithFormat:@"Message: %@", msg ? : @"Unknown error"];
NSLog(@"%@", msgFullString);
}
@Morse-Code
Morse-Code / gist:5318508
Last active December 15, 2015 20:28 — forked from irace/gist:5216137
Using Core Data as a cache; handling store migrations.
/*
Core Data is great but automatic migrations can be tricky. Migrations can take a long time, which could
result in [your app being terminated](http://stackoverflow.com/questions/13333289/core-data-timeout-adding-persistent-store-on-application-launch)
if it is happening on the main thread during application launch. Performing migrations on a background
thread is also a [bad idea](http://stackoverflow.com/a/2866725/503916), meaning your application really
needs to be able to fully launch *without a Core Data stack whatsoever* in order to safely migrate.
This can be a huge change to make to an existing app.
If you're really only using Core Data as a cache, you don't actually *need* to perform a migration.
Simply check if the existing store is compatible with your managed object model and if so, delete
@Morse-Code
Morse-Code / UntoggleableSwitch.h
Last active December 15, 2015 20:28 — forked from irace/gist:5272146
Custom UISwitch subclass to control switch state programmatically.
@interface UntoggleableSwitch : UISwitch
@end
@Morse-Code
Morse-Code / locale.m
Created April 5, 2013 11:01
Getting a locale to match the user's language preference.
/*
Getting a locale to match the user's language preference
*/
// You’d think you could just use:
[NSLocale currentLocale];
// But you’d be wrong. Instead, go with:
[[NSLocale alloc] initWithLocaleIdentifier:[NSLocale preferredLanguages][0]];
@Morse-Code
Morse-Code / removeNonAlphaNum.c
Created April 4, 2013 12:41
Remove non-alphanumeric characters from a C string.
void stringRemoveNonAlphaNum(char *str)
{
unsigned long i = 0;
unsigned long j = 0;
char c;
while ((c = str[i++]) != '\0')
{
if (isalnum(c))
{
@Morse-Code
Morse-Code / stringReverse.c
Created April 4, 2013 12:36
Reverse a C string using bitwise XOR operator.
void stringReverse(char *str)
{
char *p1, *p2;
if (!str || !*str)
{
NSLog(@"No string passed into reverse function.");
}
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
@Morse-Code
Morse-Code / gist:4122759
Created November 21, 2012 03:03 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@Morse-Code
Morse-Code / marky.py
Created November 9, 2012 13:53
marky
# Here is a Bookmarklet for running the script using the current page's URL
# as input: javascript:(function()%7Bif(document.location.href.indexOf('http')===0)document.location.href='pythonista://marky?action=run&argv='+document.location.href;%7D)();
import clipboard
import urllib2
import webbrowser
def markdownify(clipstring):
marky = 'http://heckyesmarkdown.com/go/?u='