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];
相关问题
顺便说一下,如果你想要把多个嵌套数据,如图片中,你可以使用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