gpt4 book ai didi

macos - 在osx的沙盒中创建文件夹

转载 作者:行者123 更新时间:2023-12-03 17:04:59 30 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于macos - 在osx的沙盒中创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11430346/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com