2011-08-08 103 views
6

我想從我的.Plist數組中加載我的數組,但它不工作。從.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的描述。希望這對你有所幫助。

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"]];