首页 > 其他 > 详细

解决 pathForResource 返回 nil的问题

时间:2016-09-02 00:28:34      阅读:174      评论:0      收藏:0      [点我收藏+]

点击(此处)折叠或打开

  1. NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"xml"]; 
  2. NSLog(@"path=%@",path); 
  3. NSError *error=nil; 
  4. NSString* fileText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; 
  5. NSLog(@"%@",error); 
  6. NSLog(@"path:%d , fileText:%d",[path retainCount],[fileText retainCount]);

结果在 stringWithContentsOfFile出现错误“The operation couldn’t be completed

 

追查发现是pathForResource返回nil,我查了半天没有找到解决办法,我尝试找Default.png,发现能够找到在"/Users/admin/Library/Application Support/iPhone Simulator/6.0/Applications/FFB75F5F-86B7-4B74-9CEF-9F76DE56764C/BaseAppKit.app/"目录下,而在这个目录下确没有sample.xml,我忽然想到了可能需要在项目配置文件里告诉编译器把sample.xml拷贝到BaseAppKit.app/下,

果然我找到了添加资源的地方:

 

单击项目-》Build phases-》若是没有“copy files”就需要添加:

技术分享

添加文件:

技术分享

技术分享

 

 

添加完后再运行就找到文件了

技术分享

解决 pathForResource 返回 nil的问题

原文:http://www.cnblogs.com/mafeng/p/5831628.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!