【发布时间】:2014-10-29 20:49:00
【问题描述】:
我正在尝试以以下格式以编程方式将字典添加到 plist:
根(字典) | StringOfViewID(字典) | 按钮标题(字典) | 细绳 字符串
我可以成功地做到这一点,但我想在同一个 ViewID(Dict) 下继续向 ViewID(Dict) 添加更多 ButtonTitle(Dict)。
目前我只能替换现有的。
类似这样的:
根(字典) | StringOfViewID(Dict) - ButtonTitle(Dict)(2)-String String | ButtonTitle(字典)(1) | 细绳 字符串
这是我正在使用的代码:
//Initialize and load the plist file here:
[...]
NSMutableDictionary *data;
NSMutableDictionary *viewID;
NSMutableDictionary *buttonName;
NSArray *keys;
NSArray *locations;
// Insert the data into the plist
NSNumber *xNumber = [[NSNumber alloc] initWithDouble:locationX];
NSNumber *yNumber = [[NSNumber alloc] initWithDouble:locationY];
keys = [NSArray arrayWithObjects:@"locationX", @"locationY", nil];
locations = [NSArray arrayWithObjects:xNumber, yNumber, nil];
data = [NSMutableDictionary dictionaryWithObjects:locations forKeys:keys];
buttonName = [NSMutableDictionary dictionaryWithObject:data forKey:myButtonTitle];
viewID = [NSMutableDictionary dictionaryWithObject:buttonName forKey:@"StringOfViewID"];
[viewID writeToFile:path atomically:YES];
谢谢
【问题讨论】:
-
我不太了解你的数据结构。我想你的字典太多了。看起来您可以继续将
buttonName字典条目添加到此根字典中,并且不需要“StringOfViewID”字典。 -
我实际上需要所有字典。我正在获取视图的 ID(即标签),因此它是一个不同的字典,将在每个视图上保存许多按钮,并且每个按钮都需要有它的坐标和名称。至少这对我来说是有道理的,但如果你能提出一个更好的方法,请让我有点失落......
-
您想在将 plist 文件读回内存后添加更多按钮,还是仅在第一次创建文件时添加?
-
不,我想在编写 plist 后添加更多按钮。不同的去每个视图。有些视图可能没有按钮,有些可能由用户添加。
标签: ios nsdictionary plist