gpt4 book ai didi

xcode - 将文件从应用程序包复制到用户的应用程序支持文件夹

转载 作者:行者123 更新时间:2023-12-03 16:27:17 25 4
gpt4 key购买 nike

我正在制作一个应用程序,需要将某些文件从应用程序包复制到用户的应用程序支持文件夹。这是我实际使用的代码:

NSBundle *thisBundle = [NSBundle mainBundle];
NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *userpath = [paths objectAtIndex:0];
userpath = [userpath stringByAppendingPathComponent:executableName]; // The file will go in this directory
NSString *saveFilePath = [userpath stringByAppendingPathComponent:@"db.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
[fileManager copyItemAtPath:[thisBundle pathForResource:@"db" ofType:@"sqlite"] toPath:saveFilePath error:NULL];

但这不会复制任何内容,顺便说一句,这是一个 Mac 应用程序。

最佳答案

终于可以添加了

if ([fileManager fileExistsAtPath:userpath] == NO)
[fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:nil];

结果代码是:

NSBundle *thisBundle = [NSBundle mainBundle];

NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *userpath = [paths objectAtIndex:0];
userpath = [userpath stringByAppendingPathComponent:executableName]; // The file will go in this directory
NSString *saveFilePath = [userpath stringByAppendingPathComponent:@"db.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:userpath] == NO)
[fileManager createDirectoryAtPath:userpath withIntermediateDirectories:YES attributes:nil error:nil];

[fileManager copyItemAtPath:[thisBundle pathForResource:@"db" ofType:@"sqlite"] toPath:saveFilePath error:NULL];

关于xcode - 将文件从应用程序包复制到用户的应用程序支持文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7029316/

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