It is interesting and quite uncommon that to check weather a folder exists, you should create a bool reference and pass it to the method fileExistsAtPath. This is a sample getter for a folderPath property, that I created. It can be used in various projects. It creates a folder in Documents folder of your app. Change amp to &.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
- (NSString *)folderPath { if (! _folderPath) { NSString *folderName = @"YourFolderName"; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [documentPaths objectAtIndex:0]; _folderPath = [documentsDirectoryPath stringByAppendingPathComponent:folderName]; // if folder doesn't exist, create it NSError *error = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isDir; if (! [fileManager fileExistsAtPath:_folderPath isDirectory:&isDir]) { BOOL success = [fileManager createDirectoryAtPath:_folderPath withIntermediateDirectories:NO attributes:nil error:&error]; if (!success || error) { NSLog(@"Error: %@", [error localizedDescription]); } NSAssert(success, @"Failed to create folder at path:%@", _folderPath); } } return _folderPath; } |