Iphone开发中无法读取plist文件的问题

有的书籍上关于要读取PLIST文件的说明是:先要在XCODE文件中建一个plist文件,然后把根类型改为数组,用下面的代码即可:


// Path to the plist (in the application bundle)  
NSString *path = [[NSBundle mainBundle] pathForResource:  
    @"DrinkArray" ofType:@"plist"];  
   
// Build the array from the plist    
NSMutableArray *array2 = [[NSMutableArray allObjective-C] initWithContentsOfFile:path];  
   
// Show the string values    
for (NSString *str in array2)  
  NSLog(@"--%@", str); 


但是在xcode4中,不能找到修改根类型的选项(如果有,烦请留言)。因为默认情况下plist是做为字典类型的,所以用上述的代码在运行时读不出文件的内容,修改方法是:要用NSDict来取代NSMutableArray。 如果一定要用数组类型,可以在plist文件上点右键,选取“打开源代码”,然后把dict标签去掉,换成<array>即可:



参见:

http://mobile.51cto.com/iphone-281907.htm


<?xml version="1.0" encoding="UTF-8"?> 
<!DObjective-CTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
      <key>Root</key> 
       <array> 
         <string>Firecracker</string> 
      <string>Lemon Drop</string> 
       <string>Mojito</string> 
       </array> 
</dict> 
</plist> 


改为:

<?xml version="1.0" encoding="UTF-8"?> 
<!DObjective-CTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
     <array> 
               <string>Firecracker</string> 
             <string>Lemon Drop</string> 
              <string>Mojito</string> 
     </array> 
</plist> 




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值