Q
通過代碼
0
A
回答
4
你可以這樣說:
//Create a Mutant Dictionary
NSMutableDictionary *theMutantDict = [[NSMutableDictionary alloc] init];
//Fill it with data
[theMutantDict setObject:@"John" forKey:@"Name"];
[theMutantDict setObject:@"Doe" forKey:@"Lastname"];
//Then search for cache dir
NSString *libraryDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *cacheDir = [libraryDir stringByAppendingPathComponent:@"Caches"];
//Then write the file
NSString *filePath = [cacheDir stringByAppendingString:@"/TheFile.plist"];
[theMutantDict writeToFile:filePath atomically:YES];
1
如果你有存儲在一個NSArray您的數據是,因爲這很容易:
// filling array with data ...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
NSString *plistPath = [cachePath stringByAppendingPathComponent:@"my_nsarray_data.plist"];
[klasserArray writeToFile:plistPath atomically:YES];
// other stuff ...
3
只需使用的NSArray的將writeToFile法的陣列存儲在plist中和arrayWithContentsOfFile加載它。
[self.array writeToFile:path atomically:YES];
self.array = [[NSArray arrayWithContentsOfFile:path];
// or in case you need to add/remove objects (NSMutableArray):
self.array = [[[NSArray arrayWithContentsOfFile:path] mutableCopy] autorelease];
相關問題
- 1. 通過代碼
- 2. 通過代碼
- 3. 通過代碼
- 4. 通過代碼
- 5. 通過代碼
- 6. 通過代碼
- 7. 通過代碼
- 8. 通過代碼
- 9. 通過代碼
- 10. 通過代碼
- 11. 通過代碼
- 12. 通過代碼
- 13. 通過代碼
- 14. 通過代碼
- 15. 通過代碼
- 16. 通過代碼
- 17. 通過代碼
- 18. 通過代碼
- 19. 通過代碼
- 20. 通過代碼
- 21. 通過代碼
- 22. 通過代碼
- 23. 通過代碼
- 24. 通過代碼
- 25. 通過代碼
- 26. 通過代碼
- 27. NotifyPropertyChanged通過代碼
- 28. Magento - 通過代碼
- 29. RelativeLayout通過代碼
- 30. 通過C#代碼
順便說一下,如果你想要把多個嵌套數據,如圖片中,你可以使用setObject方法把一個數組或其他字典裏突變字典。問候 – LuisEspinoza 2012-03-12 19:44:01
作品!但是說我有一個文本框和一個按鈕。當我按下按鈕時,文本字段值被存儲到文件中。但是,您然後更改文本字段中的值並再次按下按鈕。我該怎麼做,這不是在plist中被替換,而是添加? – Jacob 2012-03-13 16:08:04
在這種情況下,每次按下按鈕時都必須更改setObject中使用的「key」字符串。如果它有效,請給我一個! – LuisEspinoza 2012-03-13 16:56:09