if (NSNotFound == index) {
rather than the old:
if (index == NSNotFound) {
but I keep typing the older form. Gotta teach this old dog a new trick.
P.S. The benefit is that you can't accidentally type:
if (index = NSNotFound) {
Odd notes about developing in Cocoa and Objective-C
// use this in your init routine
[[NSUserDefaultsController sharedUserDefaultsController] addObserver: self
forKeyPath: @"values.MyPreferenceKey"
options: 0
context: NULL];
// implement this method to handle the notifications
- (void) observeValueForKeyPath: (NSString *) keyPath
ofObject: (id) object
change: (NSDictionary *) change
context: (void *) context
{
if ([keyPath isEqualToString:@"values.MyPreferenceKey"]) {
// do something in response to that change
}
}
- (void) dealloc
{
[[NSUserDefaultsController sharedUserDefaultsController] removeObserver: self
forKeyPath: @"values.MyPreferenceKey"];
[super dealloc];
}
@interface MyImageView : NSImageView
{
NSString *mImagename;
}
@end
@implementation MyImageView
- (void)setImage:(NSImage *)image
{
[image setName:[[mImagename lastPathComponent] stringByDeletingPathExtension]];
[super setImage:image];
}
- (BOOL)performDragOperation:(id)sender
{
BOOL dragSucceeded = [super performDragOperation:sender];
if (dragSucceeded) {
NSString *filenamesXML = [[sender draggingPasteboard] stringForType:NSFilenamesPboardType];
if (filenamesXML) {
NSArray *filenames = [NSPropertyListSerialization
propertyListFromData:[filenamesXML dataUsingEncoding:NSUTF8StringEncoding]
mutabilityOption:NSPropertyListImmutable
format:nil
errorDescription:nil];
if ([filenames count] >= 1) {
mImagename = [filenames objectAtIndex:0];
} else {
mImagename = nil;
}
}
}
return dragSucceeded;
}
@end
- (void) setPrintInfo:(NSPrintInfo*)info
{
[super setPrintInfo:info];
[self myCodeThatDoesSomethingWithTheNewPrintInfo];
}
A handy technique during development is to have an application automatically open the last document it used each time it's launched.
It's just that much faster to get back running the app after making a change.
Add this method to the app delegate:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSArray *urls = [[NSDocumentController sharedDocumentController] recentDocumentURLs];
if ([urls count] > 0) {
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[urls objectAtIndex:0]
display:YES];
}
}
I've never understood why some programmers continue to write C code in the form:
if (someCondition)
doSomething();
else
doSomethingElse();
as opposed to
if (someCondition) {
doSomething();
} else {
doSomethingElse();
}
Beyond saving a couple of keystrokes, the first example has absolutely no advantage over the second. While the second is much more bullet proof.
When working on code written by others that cling to the first form, I continue to run across code like this:
if (someCondition)
doSomething();
doMore();
doOtherthings();
Where the intention is that doMore() is intended to be executed only when someCondition is true.
When you look at the history of the code it almost always starts out as
if (someCondition)
doSomething();
doOtherthings();
and then later the doMore() is added.
Please, please, please take the extra split second it takes to type in the safe form of the if from day one.
NSDate *startTime = [NSDate date];
{ ... } // the operation that I'm timing
NSTimeInterval elapsedTime = -[startTime timeIntervalSinceNow];
NSLog(@"That took %f seconds", elapsedTime);