2011-11-07 4 views
1

私はこれまでに検索して読んでこれを成功させましたが、これに問題があります。Plist Array to NSArrayオブジェクトなし

私は文字列の配列であるplistを持っています。配列にオブジェクトが読み込まれることはありません。私は正常に他のコントローラの辞書のためにこれを行う。だからここに私のコードです。

// Open plist and get brands 
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"brands" ofType:@"plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
if ([fileManager fileExistsAtPath:plistPath]) { 
    NSLog(@"The file exists"); 
} else { 
    NSLog(@"The file does not exist"); 
} 

_brands = [NSArray arrayWithContentsOfFile:plistPath]; 

_catList = [[[NSMutableArray alloc] initWithCapacity:[_brands count]] autorelease]; 

ここは私のplistです。 _brands配列はNSArray *ブランドとして定義され、プロパティは非原子的、読み取り専用として設定され、ブランド= _brandsとして合成されることに注意してください。

<?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>Root</key> 
    <array> 
     <string>Brand A</string> 
     <string>Brand B</string> 
     <string>Brand C</string> 
     <string>Brand D</string> 
    </array> 
</dict> 
</plist> 

タグがありますが、Xcodeでは文字列の配列として表示されています。辞書ではありません>配列>多くの文字列

答えて

7

PListsは心臓部に辞書です(<のdict >タグに気付きます)。ファイルをNSDictionaryに読み込み、objectforKey:メソッドとRootキーを使用して辞書を辞書に尋ねます。

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath]; 
_brands = [dict objectForKey:@"Root"]; 
+0

ありがとう!私は先に進んで辞書として設定しようとしていましたが、plistを直接配列に読み込んだ古いチュートリアルとサンプルを見つけました。これは古いiOSビルドで許可されましたか? – Nungster

+0

私はそれが今まで許されたかどうかわかりません。私はいつもそれらを辞書に読みました。 –

+0

私はこのような例をたくさん見てきました:http://iphonedevelopertips.com/data-file-management/reading-a-plist-into-an-nsarray.html – Nungster

3
NSString *plistPath = [self copyFileToDocumentDirectory:plistFileName]; 
NSDictionary *plistContents=[[NSDictionary alloc] initWithContentsOfFile:plistPath]; 

[plistPath release]; 
return plistContents;