2011-08-08 10 views
6

私の.Plistの配列から配列をロードしようとしていますが、動作していません。アレイを.Plistからロードする

enter image description here

これは私が使用しているコードです:

NSString *path = [[NSBundle mainBundle]pathForResource:@"DiseasePropertyList" ofType:@"plist"]; 
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path]; 
self.myArray = rootLevel; 
[rootLevel release]; 
+8

このリストは危険です! – Moshe

答えて

9

これを試してください。ファイル名を変更してください。 正常に動作します。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *recentFilePath = [documentsDirectory stringByAppendingPathComponent:@"recent.plist"]; 
NSArray *history = [NSArray arrayWithContentsOfFile:recentFilePath]; 
+2

別の方法: '\t NSURL * url = [[NSBundle mainBundle] URLForResource:@ "DiseasePropertyList" withExtension:@ "plist"]; \t NSArray * myArray = [NSArray arrayWithContentsOfURL:url]; ' –

2
NSString *pathStr = [[NSBundle mainBundle] bundlePath]; 
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"yourproject.plist"]; 
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath]; 

を、ちょうどsettingsDictの説明をご確認

のplistは、次のようになります。これがあなたのお役に立てば幸いです。

5

あなたのplistは実際には辞書です。配列はキー "Array"の辞書のオブジェクトです。 「配列」と切り取り(CMD-x)を選択し、空のplist(CMD v)に貼り付けることで、plistをXcode 4の配列にすることができます。

+2

これはおそらく問題であると言います。 plistをソースとして見ることもできますし、 ''と ''タグをファイルから削除するだけです(いつもやっています)。 – theMikeSwan

+2

また、開始タグの後に ..タグを削除してください。これは間違いなく私の問題を解決します.... – DShah

2

あなたのplistファイルは、アプリケーションバンドル内にある場合、最も簡単にはこれです:

NSArray *myArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename_without_extension" ofType:@"plist"]]; 
0

は、それが私のためにうまく働いたこのメソッドを使用します。

NSArray *country= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryList" ofType:@"plist"]]; 
関連する問題