[英]Reading and Writing NSMutableArray to iPhone not working (Works on iOS Simulator)
在我正在開發的應用程序中,我想要一個NSMutableArray(稱為pathArray),可以從應用程序目錄中的文件中讀取該消息,並能夠創建該數組的實例,以便向其中添加對象和/或從中刪除對象,然后我想將其寫回到文件中。 我有一個UILabel,它顯示此數組中的內容數。 我的問題:我的以下代碼在Xcode的iOS模擬器上可以正常工作,但是當我嘗試在實際的iPhone上運行該應用程序時,數據未保存。 我知道這里有很多與此問題相關的問題,但是我似乎看不到我在做什么錯。 任何幫助將不勝感激。
- (void) loadArrayContents {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingString:@"theArray"];
//Objects contained in an array returned by 'initWithContentsOfFile' are immutable even if the array is mutable
NSArray* contentsArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
pathsArray = [[NSMutableArray alloc] initWithArray:contentsArray];
}
和...
- (void) saveArrayContents {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingString:@"theArray"];
[pathsArray writeToFile:filePath atomically:YES]);
}
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"theArray"];
應該解決問題。 問題所在
NSString *filePath = [documentsDirectory stringByAppendingString:@"theArray"];
是它沒有在文件路徑中添加/
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.