NSBundle pathForResource取不到值如何解决?

代码如下:


  1. NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; 
  2.       NSLog(@"path = %@", path); 
  3.       NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 

但是运行出来,path一直是null

师父找了好久,终于找到答案了

http://*.com/questions/3949368/nsbundle-pathforresource-is-null

记录一下:

command-line tool是没有bundle的,所以用NSBundle的时候一直是null, 如果要使用bundle获取资源,那么需要添加application的target。

按照上述改法,添加了一个target之后,还有问题,依旧找不到文件

然后师父做了以下操作:

NSBundle pathForResource取不到值如何解决?

在这里添加了文件之后,然后再运行,文件就能取到了。

之后又尝试了在command-line tool中也按照上述方法,添加文件

首先,默认是没有bundle选项的

NSBundle pathForResource取不到值如何解决?

其次,就算手动添加了build phrase,再添加文件,运行出来,还是取不到path

NSBundle pathForResource取不到值如何解决?

所以, 如果要使用bundle获取项目中的资源, 必须要使用application