2012-05-19 178 views
0

我發現了一些類似的話題,但沒有一個解決我的問題。 基本上我想從plist中讀取我的數組中的nsstring,然後我想覆蓋nsstring的值。數組無法從plist讀取數據

NSArray *directoriespaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDir = [directoriespaths objectAtIndex:0]; 
NSString *fullnamefordir = [documentsDir stringByAppendingPathComponent:@"data.plist"]; 

     NSArray *turns = [NSArray arrayWithContentsOfFile:fullnamefordir]; 
     NSString *resultofarray = [turns objectAtIndex:0]; 
     NSLog(@"%@",turns); 
     NSLog(@"%@",resultofarray); 

的NSLog總是顯示爲空,我並不想保存任何到目前爲止,因爲我甚至無法讀取值:/

<?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>8</string> 
</array> 
</plist> 

我也試圖與NSDictionary中,但它也沒有按」 t工作 在此先感謝:)

回答

3

首先檢查您的plist文件是否存在於文檔目錄中。

NSFileMangaer *objFileManager=[[NSFileManager alloc]init]; 
BOOL isfileExist=[objFileManager fileExistsAtPath:yourDocumentDirectoryPath]; 

IF布爾值是true,然後檢查你的內容的plist的。 可能文件不存在於文檔目錄中,它在資源中。 如果您沒有將plist從資源文件夾複製到文檔目錄,請先將其複製到那裏,然後嘗試訪問它。

+0

nslog顯示該文件確實存在。將文件從資源複製到文檔目錄是什麼意思,我在哪裏可以找到這個文件夾? – repoguy

+1

檢查你的plist文件的文檔目錄。 YourUsername-> Library-> ApplicationSupport-> IphoneSimulator-> 5.0-> ChooseApplicationID-> Documents 如果有許多應用程序availabel刪除所有並運行應用程序..只有一個應用程序將在那裏,然後檢查文檔目錄。 – Cyrus1

+0

就像你說的,我刪除了所有的應用程序文件,我再次構建我的應用程序。奇怪的是,現在我的plist文件不存在於Documents中,並且nslog顯示文件不存在。在這種情況下我該怎麼辦?順便謝謝你的回覆。 – repoguy