NSBundle pathForResource取不到值如何解決?
代碼如下:
- NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
- NSLog(@"path = %@", path);
- NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
但是運(yùn)行出來,path一直是null
師父找了好久,終于找到答案了
http://stackoverflow.com/questions/3949368/nsbundle-pathforresource-is-null
記錄一下:
command-line tool是沒有bundle的,所以用NSBundle的時(shí)候一直是null, 如果要使用bundle獲取資源,那么需要添加application的target。
按照上述改法,添加了一個(gè)target之后,還有問題,依舊找不到文件
然后師父做了以下操作:
在這里添加了文件之后,然后再運(yùn)行,文件就能取到了。
之后又嘗試了在command-line tool中也按照上述方法,添加文件
首先,默認(rèn)是沒有bundle選項(xiàng)的
其次,就算手動(dòng)添加了build phrase,再添加文件,運(yùn)行出來,還是取不到path
所以, 如果要使用bundle獲取項(xiàng)目中的資源, 必須要使用application