对 NSBundle 的行为感到困惑
我有一个 plist 文件,位于 .app 目录的 Templates 目录中。因此,这个 plist 文件的加载没有任何问题:
NSDictionary *sqlPropertyList = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:CH_PQ_QUERIES ofType:@"plist"]];
我在同一目录中创建了一个 .txt 文件,并尝试以相同的方式加载它:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"excel2004" ofType:@"xml"];
但是 filePath
始终为空,即使捆绑的似乎已加载。
这是怎么回事?为什么允许 plist 位于子目录中而其他文件则不允许? plist 文件是否是意外加载的,我不应该依赖这种行为?
谢谢
I've a plist file that sits in a directory called Templates of my .app directory. This plist file is loaded thus without any problems:
NSDictionary *sqlPropertyList = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:CH_PQ_QUERIES ofType:@"plist"]];
I created a .txt file in the same directory and tried to load it the same way:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"excel2004" ofType:@"xml"];
But filePath
was always null, even though the bundled appeared to be loaded.
What's going on here? Why are plists allowed to sit in subdirectories and other files not? Is the plist file being loaded by accident and I shouldn't rely on this behaviour?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是一个愚蠢的问题,但您说您创建了一个“txt”文件,但您对
pathToResource:ofType:
的调用指定xml
作为类型。这是正确的吗?当然,任何类似的文件都需要位于应用程序包的Resources
目录中。This might be a silly question, but you say you created a "txt" file but your call to
pathToResource:ofType:
specifiesxml
as the type. Is that correct? And, of course, any files like these need to be in theResources
directory of your app bundle.