解决 pathForResource 返回 nil的问题

 今天想试验解析xml文件,在项目下建了一个xml文件,代码如下

 

 
 
  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”就需要添加:

 

添加文件:

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值