我将数据保存到这样的plist:
- (IBAction)save:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"data.plist"];
NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == data) {
data = [[NSMutableArray alloc] initWithCapacity:0];
}
else {
[data retain];
}
NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
[array setObject:label1.text forKey:@"Test1"];
[array setObject:label2.text forKey:@"Test2"];
[array setObject:label3.text forKey:@"Test3"];
[array setObject:label4.text forKey:@"Test4"];
NSLog(@"%@", array);
[data addObject:array];
[array release];
[data writeToFile:plistPath atomically: TRUE];
[data release];
}
的NSLog:
2012-07-01 18:52:19.566 testapp[22651:707] {
Test1 = 40;
Test2 = 102;
Test3 = 153;
Test4 = 255;
}
如何将保存的数据加载到uitableview
?
答案 0 :(得分:1)
要阅读它,请执行以下操作:
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
然后,您应该能够读取所有键的值,如下所示:
NSString *myValue = [plistDict objectForKey:@"someKey"];
修改强>
重新阅读您的代码后,您似乎可能正在做一些您不想做的事情。您当前的plist结构是一个顶级数组,每次调用“save”时都会向自己添加一个字典。这是你想要做的吗?如果是这样,代表表格中的单元格,数组中的索引是什么?
如果是,那么你想做
// top level array
NSMutableDictionary* plistArray = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
// individual dictionary at one array index (such as indexPath.row in cellForRowAtIndexPath)
NSMutableDictionary *dictionary = [plistArray objectAtIndex:yourIndex];
在任何情况下,您命名为“array”的对象实际上都是一个字典,因此您应该重命名它(或者将其更改为数组,如果这是您想要的那样)。