1

使用以下代码获取文件路径:

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 中自动创建文件夹?

4

1 回答 1

2

试一试:[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];

该方法不仅会创建指定路径的目录,还会创建任何可能不存在的中间目录。

于 2012-07-12T05:47:24.213 回答