Skip to content

Instantly share code, notes, and snippets.

View mikeabdullah's full-sized avatar

Mike Abdullah mikeabdullah

View GitHub Profile
@mikeabdullah
mikeabdullah / KSIsNotNSNotFound.h
Created January 17, 2011 14:31
Simple value transfer like `NSIsNotNil`, but for NSNotFound
//
// KSIsNotNSNotFound.h
// Sandvox
//
// Created by Mike on 16/01/2011.
// Copyright 2011 Karelia Software. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@mikeabdullah
mikeabdullah / gist:866873
Created March 12, 2011 00:47
How to make a managed object complain if it's being turned into a fault while KVO observers are still registered with it.
- (void)willTurnIntoFault;
{
[super willTurnIntoFault];
if ([self observationInfo])
{
NSLog(@"%@ has observers:\n%@", [self objectID], [self observationInfo]);
}
}
@mikeabdullah
mikeabdullah / gist:866907
Created March 12, 2011 01:21
How to fix node insertion in NSTreeController on Leopard
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5
- (void)didInsertNode:(NSTreeNode *)node;
{
// See if model matches tree state. Leopard gets them out of sync, adding to the model rather than inserting
id object = [node representedObject];
NSIndexPath *indexPath = [node indexPath];
NSUInteger index = [indexPath lastIndex];
NSTreeNode *parent = [node parentNode];
@mikeabdullah
mikeabdullah / gist:867275
Created March 12, 2011 14:35
Log errors to the console before they're presented
- (NSError *)application:(NSApplication *)theApplication
willPresentError:(NSError *)error
{
// Log the error to the console for debugging
NSLog(@"Application will present error:\n%@", [error description]);
return error;
}
@mikeabdullah
mikeabdullah / gist:867278
Created March 12, 2011 14:37
More informative debugging description for NSError
- (NSString *)debugDescription;
{
// Log the entirety of domain, code, userInfo for debugging.
// Operates recursively on underlying errors
NSMutableDictionary *dictionaryRep = [[self userInfo] mutableCopy];
[dictionaryRep setObject:[self domain]
forKey:@"domain"];
[dictionaryRep setObject:[NSNumber numberWithInteger:[self code]]
@mikeabdullah
mikeabdullah / gist:867284
Created March 12, 2011 14:41
Apple's recommended way to disable undo registration for NSManagedObjectContext
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] disableUndoRegistration];
// Make your special changes to the managed object
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] enableUndoRegistration];
@mikeabdullah
mikeabdullah / gist:867285
Created March 12, 2011 14:44
More general way to disable undo registration, which happens to be compatible with NSManagedObjectContext
[[NSNotificationCenter defaultCenter]
postNotificationName:NSUndoManagerCheckpointNotification
object:undoManager];
[undoManager disableUndoRegistration];
// Make your special changes to the managed object
[[NSNotificationCenter defaultCenter]
postNotificationName:NSUndoManagerCheckpointNotification
object:undoManager];
[undoManager enableUndoRegistration];
@mikeabdullah
mikeabdullah / gist:868549
Created March 13, 2011 23:27
Customising object removal from NSArrayController
- (void)willRemoveObject:(id)object;
{
// Put your removal handling code here
}
- (void)removeObjects:(NSArray *)objects
{
// -removeObject: calls this method internally.
// Iterate the objects, reporting each one
@mikeabdullah
mikeabdullah / gist:878354
Created March 20, 2011 14:37
Testing if an NSTreeNode is a descendant of another
- (BOOL)ks_isDescendantOfNode:(NSTreeNode *)aNode;
{
NSTreeNode *testNode = self;
while (testNode)
{
if (testNode == aNode) return YES;
testNode = [testNode parentNode];
}
return NO;
@mikeabdullah
mikeabdullah / gist:905412
Created April 6, 2011 09:54
How to fake removing a recent document URL. Note, uses -ks_isEqualToURL: method from KSFileUtilities
- (void)clearRecentDocumentURL:(NSURL *)url;
{
NSArray *recentDocs = [self recentDocumentURLs];
[self clearRecentDocuments:self];
// Must enumerate backwards to register in the correct order
for (NSURL *aURL in [recentDocs reverseObjectEnumerator])
{
if (![aURL ks_isEqualToURL:url])
{