Skip to content

Instantly share code, notes, and snippets.

View mikeabdullah's full-sized avatar

Mike Abdullah mikeabdullah

View GitHub Profile
@mikeabdullah
mikeabdullah / gist:7864827
Created December 8, 2013 22:50
ASCII filename normalization
- (NSString *)normalizeFilename:(NSString *)filename;
{
NSMutableString *result = [filename mutableCopy];
CFStringTransform((CFMutableStringRef)buffer,
NULL,
CFSTR("Any-Latin; Latin-ASCII"),
false);
return [result autorelease];
@mikeabdullah
mikeabdullah / gist:7034158
Last active December 25, 2015 20:19
-hostURL
// A single slash takes us back to the host's root directory,
// discarding any components after the path.
// Then make absolute so callers don't have to worry about any
// side-effects of working with relative URL objects.
return [[NSURL URLWithString:@"/" relativeToURL:url] absoluteURL];
@mikeabdullah
mikeabdullah / gist:6685381
Created September 24, 2013 14:13
Build date for Cocoa apps
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[formatter setDateFormat:@"MMM d yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *string = @__DATE__;
NSDate *buildDate = [formatter dateFromString:string];
[formatter release];
@mikeabdullah
mikeabdullah / gist:6129947
Created August 1, 2013 09:42
Escaping a URL host name
CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)path,
NULL,
CFSTR("@:/?#"),
kCFStringEncodingUTF8);
@mikeabdullah
mikeabdullah / gist:6007775
Created July 16, 2013 11:05
Escaping a URL query to avoid confusion over + characters
CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)query,
NULL,
CFSTR("+#"),
kCFStringEncodingUTF8);
@mikeabdullah
mikeabdullah / gist:5983511
Created July 12, 2013 10:47
Correct escaping of a URL path
CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)path,
NULL,
CFSTR(";?#"),
kCFStringEncodingUTF8);
@mikeabdullah
mikeabdullah / gist:5983448
Last active December 19, 2015 16:28
INCORRECT escaping of a URL path
NSString *escaped = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
@mikeabdullah
mikeabdullah / gist:5501462
Created May 2, 2013 10:53
LIFO queue implemented (perhaps evilly) using a single block
/* This code is devious and perhaps even evil. Manages a "stack" of completion
* handlers by actually only having a single block. When the block runs, it
* replaces itself with the next one down the stack.
*/
- (void)pushCompletionHandler:(void (^)(NSError*))block;
{
NSParameterAssert(block);
id previousHandler = _completionHandler;
@mikeabdullah
mikeabdullah / gist:5367503
Created April 11, 2013 21:50
Mixing -foo and -isFoo method on NSManagedObject
@interface Bar : NSManagedObject
@property NSManagedObject *foo;
@end
@implementation Bar
@dynamic foo;
- (BOOL)isFoo { return NO; }
@mikeabdullah
mikeabdullah / gist:4701971
Last active December 12, 2015 02:49
Retrieving bookmark data's last known path
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_8
#define NSURLPathKey @"_NSURLPathKey"
#endif
NSDictionary *values = [[NSURL resourceValuesForKeys:@[NSURLPathKey]
fromBookmarkData:bookmarkData]
NSString *path = [values objectForKey:NSURLPathKey];
#undef NSURLPathKey