Skip to content

Instantly share code, notes, and snippets.

View mikeabdullah's full-sized avatar

Mike Abdullah mikeabdullah

View GitHub Profile
@mikeabdullah
mikeabdullah / gist:996823
Created May 28, 2011 12:12
-ks_stringByIncrementingPath example
- (NSString *)firstAvailableFilename:(NSString *)filename
{
while (![self isFilenameAvailable:filename])
{
filename = [filename ks_stringByIncrementingPath];
}
}
@mikeabdullah
mikeabdullah / gist:1130831
Created August 7, 2011 21:49
Scaling images for the web, maintaining colorspace when reasonable
- (CGImageRef)renderImage:(CIImage *)scaledImage fromSource:(CIImage *)sourceImage;
{
// We want sRGB as fallback output colorspace
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CIContext *context = [CIContext contextWithCGContext:nil
options:[NSDictionary dictionaryWithObjectsAndKeys:
colorSpace, kCIContextOutputColorSpace,
nil]];
@mikeabdullah
mikeabdullah / gist:1306274
Created October 22, 2011 17:48
Forcing true reset of managed object context
- (BOOL)hardResetContext:(NSManagedObjectContext *)context store:(NSPersistentStore *)store;
{
NSPersistentStoreCoordinator *storeCoordinator = [context persistentStoreCoordinator];
if (![storeCoordinator removePersistentStore:myStore error:NULL])
{
return NO;
}
return [storeCoordinator addPersistentStoreWithType:[store type]
configuration:nil
@mikeabdullah
mikeabdullah / gist:1360468
Created November 12, 2011 12:27
Restoring an optional, secondary document window
- (void)restoreDocumentWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler;
{
if ([identifier isEqualToString:@"Secondary"])
{
// Rely on main controller to create secondary window
if (![[self windowControllers] count]) [self makeWindowControllers];
MyWindowController *mainController = [[self windowControllers] objectAtIndex:0];
completionHandler([[mainController secondaryWindowController] window], nil);
}
else
@mikeabdullah
mikeabdullah / gist:1360474
Created November 12, 2011 12:32
Setting window identifiers in code
@implementation MySecondaryWindowController
- (void)windowDidLoad
{
[super windowDidLoad];
[[self window] setIdentifier:@"Secondary"];
}
@end
@mikeabdullah
mikeabdullah / gist:1483787
Created December 16, 2011 00:39
Clearing undo manager's history while keeping document marked as edited
- (void)clearUndoHistoryOfDoc:(NSDocument *)doc
{
NSUndoManager *undoManager = [doc undoManager];
// Post a checkpoint first so any pending registrations
// (e.g. from Core Data) are committed
[[NSNotificationCenter defaultCenter]
postNotificationName:NSUndoManagerCheckpointNotification
object:undoManager];
@mikeabdullah
mikeabdullah / gist:1514233
Created December 23, 2011 13:40
A convenience for NSAlert construction
@implementation NSAlert (Karelia)
- (void)ks_setMessageTextWithFormat:(NSString *)format, ...;
{
va_list argList;
va_start(argList, format);
NSString *formatted = [[NSString alloc] initWithFormat:format arguments:argList];
va_end(argList);
[self setMessageText:formatted];
@mikeabdullah
mikeabdullah / gist:1575537
Created January 7, 2012 18:14
Adjusting -[NSDocument windowForSheet] to handle multiple sheets at once
- (NSWindow *)windowForSheet;
{
NSWindow *result = [super windowForSheet];
NSWindow *sheet;
while ((sheet = [result attachedSheet]))
{
result = sheet;
}
@mikeabdullah
mikeabdullah / gist:2206650
Created March 26, 2012 17:18
Unsafe cross-thread passing of an error
- (BOOL)saveAndReturnError:(NSError **)error
{
NSManagedObjectContext *context = [self managedObjectContext];
__block BOOL result;
[context performBlockAndWait:^{
result = [context save:error];
}];
return result;
@mikeabdullah
mikeabdullah / gist:2209110
Created March 26, 2012 19:42
Safe cross-thread passing of an error
- (BOOL)saveAndReturnError:(NSError **)error
{
NSManagedObjectContext *context = [self managedObjectContext];
__block BOOL result;
[context performBlockAndWait:^{
result = [context save:error];
if (!result && error) [*error retain];
}];
if (!result && error) [*error autorelease];