2011-12-08 38 views
0

我是Objective-C的新手,所以对于我正在做的事情可能有一个非常简单的解决方案。说我遇到的问题是,由于某种原因,下面的代码不会从我的plist文件添加内容到我的数组:plist不会加载到数组中

在我的viewDidLoad功能:

// Grab information from the plist 
NSBundle *bundle = [NSBundle mainBundle]; 
NSString *plistPath = [bundle 
    pathForResource:@"conversions" 
    ofType:@"plist" 
]; 
NSArray *array = [[NSArray alloc] 
    initWithContentsOfFile:plistPath 
]; 

文件“的转换。的plist”,在同一个文件夹上面的代码:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>Categories</key> 
    <array> 
     <dict> 
      <key>name</key> 
      <string>Speed</string> 
      <key>conversions</key> 
      <array> 
       <dict> 
        <key>type</key> 
        <string>mph</string> 
        <key>description</key> 
        <string>mile/hour</string> 
        <key>multiplier</key> 
        <integer>1</integer> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>ft/s</string> 
        <key>description</key> 
        <string>foot/second</string> 
        <key>multiplier</key> 
        <real>1.466667</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>km/h</string> 
        <key>description</key> 
        <string>kilometer/hour</string> 
        <key>multiplier</key> 
        <real>1.609344</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>m/s</string> 
        <key>description</key> 
        <string>meter/second</string> 
        <key>multiplier</key> 
        <real>0.44704</real> 
       </dict> 
      </array> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Distance</string> 
      <key>conversions</key> 
      <array> 
       <dict> 
        <key>type</key> 
        <string>ft</string> 
        <key>description</key> 
        <string>feet</string> 
        <key>multiplier</key> 
        <integer>1</integer> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>yd</string> 
        <key>description</key> 
        <string>yard</string> 
        <key>multiplier</key> 
        <real>0.3333333</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>mi</string> 
        <key>description</key> 
        <string>mile</string> 
        <key>multiplier</key> 
        <real>0.0001894</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>in</string> 
        <key>description</key> 
        <string>inch</string> 
        <key>multiplier</key> 
        <integer>12</integer> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>m</string> 
        <key>description</key> 
        <string>meter</string> 
        <key>multiplier</key> 
        <real>0.3048</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>km</string> 
        <key>description</key> 
        <string>kilometer</string> 
        <key>multiplier</key> 
        <real>0.0003048</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>cm</string> 
        <key>description</key> 
        <string>centimeter</string> 
        <key>multiplier</key> 
        <real>30.48</real> 
       </dict> 
      </array> 
     </dict> 
     <dict> 
      <key>name</key> 
      <string>Volume</string> 
      <key>conversions</key> 
      <array> 
       <dict> 
        <key>type</key> 
        <string>ft^3</string> 
        <key>description</key> 
        <string>cubic feet</string> 
        <key>multiplier</key> 
        <integer>1</integer> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>liter</string> 
        <key>description</key> 
        <string>liter</string> 
        <key>multiplier</key> 
        <real>28.31685</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>acre ft</string> 
        <key>description</key> 
        <string>acre foot</string> 
        <key>multiplier</key> 
        <real>2.3e-05</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>barrel</string> 
        <key>description</key> 
        <string>barrel [US, liquid]</string> 
        <key>multiplier</key> 
        <real>0.2374768</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>gallon</string> 
        <key>description</key> 
        <string>gallon [US, liquid]</string> 
        <key>multiplier</key> 
        <real>29.92208</real> 
       </dict> 
       <dict> 
        <key>type</key> 
        <string>quart</string> 
        <key>description</key> 
        <string>quart [US, liquid]</string> 
        <key>multiplier</key> 
        <real>29.92208</real> 
       </dict> 
      </array> 
     </dict> 
    </array> 
</dict> 
</plist> 
+2

您无法一次将整个plist文件读取到数组中。 我想你将不得不使用密钥获取它,然后将其存储到NSMutableDictionary中。 –

回答

7

在我看来,在你的plist根对象是一个字典,而不是一个数组,你不应该将其加载到NSDictionary的?

NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];