使用以下代码获取文件路径:
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:executableName];
然后我想将数据保存到文件(settings.bundle)
NSString *filePath = [path stringByAppendingPathComponent:@"settings.bundle"];
[someString writeToFile:settingsPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
错误:
Error Domain=NSCocoaErrorDomain Code=4 "The folder “settings.bundle” doesn’t exist." UserInfo=0x7ff8894cecc0 {NSFilePath=/Users/xxxx/Library/Application Support/AppName/settings.bundle, NSUserStringVariant=Folder, NSUnderlyingError=0x7ff8894a1750 "The operation couldn’t be completed. No such file or directory"}
但如果我mkdir AppName手册没有错误:
mkdir /Users/xxxx/Library/Application Support/AppName
所以我的问题是:Cocoa无法在osx中自动创建文件夹?
答案 0 :(得分:2)
给这一点:[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
该方法不仅会创建指定路径的目录,还会创建可能不存在的任何中间目录。