2009-08-27 71 views
2

目錄有什麼不好呢?創建在iPhone

#define AUDIO_NOTES_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/myApp/Pictures"] 

NSFileManager *NSFm= [NSFileManager defaultManager]; 
BOOL isDir=YES; 

if(![NSFm fileExistsAtPath:FILEPATH isDirectory:&isDir]) 
    if(![NSFm createDirectoryAtPath:FILEPATH attributes:nil]) 
     NSLog(@"Error: Create folder failed"); 
+0

FILEPATH將是未定義的。也許你的意思是AUDIO_NOTES_FOLDER? – 2009-08-27 00:25:48

+0

是的,其實這是... – Mathieu 2009-08-27 16:55:11

回答

11

文件路徑標記未定義 - 您#define AUDIO_NOTES_FOLDER在你的文件的開頭,然後用FILEPATH中,而不是你的代碼。

還要注意的是NSHomeDirectory()不一定是再找到文檔目錄的推薦的方法 - 而不是你可能想:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];
13

createDirectoryAtPath:attributes:已被棄用,而應該使用:

NSString *dirToCreate = [NSString stringWithFormat:@"%@/newDirectory",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]]; 
    NSError *error = nil; 
    BOOL isDir; 
    if(![fm fileExistsAtPath:dirToCreate isDirectory:&isDir]) 
     if(![fm createDirectoryAtPath:dirToCreate withIntermediateDirectories:YES attributes:nil error:&error]) 
      NSLog(@"Error: Create folder failed");