2011-08-04 9 views
0

私は配列であるplistを持っています、ファイル名はstartups.plistと呼ばれています。私は、次のコードで配列にそれを読むことを試みた:plistの問題を読む

NSString *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"]; 
NSMutableArray * wordList = [[NSMutableArray alloc] initWithContentsOfFile:path]; 

しかし、wordListには、ここでは常に0です。

/var/mobile/Applications/CCBF94D9-389C-4D63-B023-F39653FDCEF4/My.app/startups.plist 

をここで構造がPLISTのように見えるものです::

enter image description here

配列のサイズは何午前0 あるデバイス上でテストし、そしてそれは私を与えることは時にパスを印刷私はここで間違っている?

+0

'path'の値(と内容)は何ですか? – fbrereto

+0

私はそれを上に持っています..それはvar/mobile/Applications/,,,,, – aherlambang

答えて

2

initWithContentsOfFile:はplistファイルを開きません。

NSString *errorDesc = nil; 
NSPropertyListFormat format; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"]; 
NSData  *plistXML = [[NSFileManager defaultManager] contentsAtPath:path]; 
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization      
              propertyListFromData:plistXML 
mutabilityOption:NSPropertyListMutableContainersAndLeaves 
              format:&format 
              errorDescription:&errorDesc]; 
NSLog(@"%@",[temp objectForKey:@"Root"]); 
+0

素晴らしいです!ありがとう...私はチュートリアルを読んでいて、彼らはinitWithContentsOfFileを使用します。 – aherlambang

+0

あなたは大歓迎です。 'NSDictionary'の' initWithContentsOfFile'メソッドを使うとそれを開くかもしれませんが、私はこのメソッドを好む。 – VenoMKO

1

あなたのplistのが配列であるキーのルートを持っている辞書であるため、NSDictionaryを使用する必要があります。ここではサンプルです。

NSString *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"]; 
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 

NSMutableArray *wordList = (NSMutableArray *)[dict objectForKey:@"Root"]; 

キャストについてはわかりません。NSMutableArrayです。あなたはしなければならないかもしれない

+2

これは間違っています。 "ルート"は辞書のキーではなく、単にプロパティリストのルートオブジェクトを示します。 – Caleb

+0

私はplistののXMLは次のようになり、この構造を再作成しようとしている場合: ルート

関連する問題