gpt4 book ai didi

iphone - bundle 访问中的sqlite db路径?

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

我想将我的 sqlite db 放入一个自定义包中,并在多个项目中使用该包。

我已经从 Mac OS X FrameWork&Library -> Bundle 创建了一个新目标,我已经构建了目标,并且我已经将我的 sqlite db 放入了包中。
我已将 bundle 添加到我的 iOS 项目并尝试从中读取数据库。

现在问题来了:我可以获取包并读取它的标识符,但是当我尝试获取 db 的路径时,我得到空返回字符串。

读取数据库路径的代码:

NSBundle *myDbBundle  = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:@"myDbBundle" ofType:@"bundle"]];
NSLog(@"BundleIdentifier:%@",[myDbBundle bundleIdentifier]);
NSString *databasePath = [myDbBundle pathForResource:@"myDbName" ofType:@"sqlite"];
NSLog(@"DataBasePath:%@",databasePath);

任何关于这个问题的建议、文档或教程都是受欢迎的。

更新:
似乎我无法加载 bundle ,这就是为什么当我尝试从中读取时失败的原因。

代码:
    NSError *error;
NSLog(@"LoadBundle:%@",[myDbBundle loadAndReturnError:&error]?@"yes":@"no");
NSLog(@"Error:%@",[error localizedDescription]);

所以真正的问题是我如何在女巫中创建一个 bundle 我放置一个sqlite db并且可以从ios应用程序中读取女巫?为了创建包,我在 Mac OS X 下使用了 xcode 模板,所以我认为我必须在build设置中进行一些更改,但我不知道是什么。

最佳答案

NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"myDbName" ofType:@"sqlite"];

应该足以在主包中获取 myDbName.sqlite 的路径

编辑:
好的,这就是我所做的,首先,New File -> Resources -> Settings Bundle,创建一个新的,将其命名为 myDbBundle,然后右键单击该新创建的 bundle,在其中添加 myDbName.sqlite,确保它不是在 en.lproj 里面。此外,请确保在创建新包时勾选目标以将其包含到当前目标中,因为这不是 Settings.bundle,您想要包含它。然后运行你的代码,它工作正常。

enter image description here

这是我使用的代码
NSBundle* bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"myDbBundle" ofType:@"bundle"]];
NSLog(@"%@", bundle);
NSString* test = [bundle pathForResource:@"myDbName" ofType:@"sqlite"];
NSLog(@"%@", test);
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:@"Root" ofType:@"plist"]];
NSLog(@"%@", dict);
NSLog(@"%@", dict);正在打印出正确的值,这意味着它已成功加载

关于iphone - bundle 访问中的sqlite db路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9002208/

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