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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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];

Generate the list yourself:

$ cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/
  Developer/SDKs/iPhoneOS*.sdk/System/Library/Frameworks/UIKit.framework/Headers
$ grep -H UI_APPEARANCE_SELECTOR ./* | sed 's/ __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;//'

UIActivityIndicatorView

@Morse-Code
Morse-Code / ToggleMapType.m
Created April 29, 2013 03:03
MapKit map type three way toggle.
- (IBAction)toggleMapType:(id)sender
{
NSLog(@"toggleMapType");
mapType = (mapType + 1) % 3;
if (mapType == 0) {
self.dangerMap.mapType = MKMapTypeStandard;
}
else if (mapType == 1) {