2012-05-04 9 views
0

私はplsをdocsdirにコピーしてからそれを可変配列に読み込むアプリケーションを書いています。ただし、以下のコードでは配列の数が0になります。ただし、辞書ログの行は正しい項目を返します。また、ファイルがdocsdirにコピーされていることを確認しました。arrayWithFileは0のカウントを返します

-(NSString *)docsDir { 
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSString *listPath = [[self docsDir]stringByAppendingPathComponent:@"list.plist"]; 
    if (![[NSFileManager defaultManager]fileExistsAtPath:listPath]) { 
     [[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"list" ofType:@"plist"] toPath:listPath error:nil]; 
     NSLog(@"Chicken"); 
    } 

    NSLog(@"%@", [NSDictionary dictionaryWithContentsOfFile:listPath]); 
    _array = [NSArray arrayWithContentsOfFile:listPath]; 

    NSLog(@"Count: %i", [_array count]); 

} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

答えて

1

を試してみてください。

右ソースコードとして開く]をクリックして選択し、あなたのファイルは次のようになります。

<?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>foo</key> 
    <array> 
     <string>foo1</string> 
     <string>foo2</string> 
     <string>foo3</string> 
    </array> 
</dict> 
</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"> 
<array> 
    <string>foo1</string> 
    <string>foo2</string> 
    <string>foo3</string> 
</array> 
</plist> 

ルート要素は配列です。いつものように編集できるようになりました。

0

plistファイルをXcodeで開きます。

左上隅に「キー」があり、plistにTypeがあります。

Sample Plist file

Typeが配列であることを確認してください。

0

あなたのplistは配列を含む辞書だと思います。
はのplistファイルのデフォルトのルート要素で辞書であるため、通常、これは、この

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:listPath] 
NSArray *array = [dict objectForKey:@"key"]; 
0

プレースメントリストは、通常の辞書に保存されます。

はここに例を示します

<dict> 
    <key>tiltingAnim</key> 
    <dict> 
     <key>filenamePrefix</key> 
     <string>radar_</string> 
     <key>delay</key> 
     <real>0.25</real> 
     <key>animationFrames</key> 
     <string>1,2,3,4,5,5,4,3,2,1,2,3,4,5</string> 
    </dict> 
    <key>takingAHitAnim</key> 
    <dict> 
     <key>filenamePrefix</key> 
     <string>radar_</string> 
     <key>delay</key> 
     <real>0.1</real> 
     <key>animationFrames</key> 
     <string>5,5,7,8,8</string> 
    </dict> 
    <key>blowingUpAnim</key> 
    <dict> 
     <key>filenamePrefix</key> 
     <string>radar_</string> 
     <key>delay</key> 
    <real>0.2</real> 
    <key>animationFrames</key> 
    <string>5,6,7,8,9,10,11,12,13,14,15,16,17</string> 
    </dict> 
    <key>transmittingAnim</key> 
    <dict> 
     <key>filenamePrefix</key> 
     <string>radar_</string> 
     <key>delay</key> 
     <real>0.3</real> 
     <key>animationFrames</key> 
     <string>5,6,5,6,5,6,5</string> 
    </dict> 
</dict>  

は今、あなたの質問に2つのソリューションがあります。

  1. コンテンツを辞書に入れて、特定のキーの配列を取り出します。
  2. plistをテキストエディタで開き、ルートキーを変更してからrootに変更します。 plistが静的でバンドルリソースにあれば、これを行うことができますが、コードによって生成されたplistの場合は、これをお勧めしません。
関連する問題