365Cocoa

A snippet of Objective-C code per day, 365 days long.
Written by Pieter Omvlee, developer and owner of Bohemian Coding.

Apr 10

Day 41: createDirectoryIfNecessary:

Another shorthand (this time on NSFileManager) that’ll explain itself:

- (BOOL)createDirectoryIfNecessary:(NSString *)directory
{
  BOOL isDir = NO;
  if ([self fileExistsAtPath:directory isDirectory:&isDir] && isDir) return YES;
  if ([self fileExistsAtPath:directory isDirectory:&isDir] && !isDir) return NO;
  
  return [self createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL];
}