问题描述
我正在尝试以编程方式将字典添加到plist中,格式如下:
I am trying to add a dictionary to a plist programmatically in the following format:
根(字典)|StringOfViewID(Dict)|ButtonTitle(字典)|细绳字符串
Root(Dict)|StringOfViewID(Dict)|ButtonTitle(Dict)|StringString
我可以成功完成此操作,但我想在同一ViewID(Dict)下继续向ViewID(Dict)添加更多ButtonTitle(Dict).
I can successfully do this but I want to keep adding to the ViewID(Dict) more ButtonTitle(Dict) under the same ViewID(Dict).
到目前为止,我只能替换现有的.
So far I can only replace the existing.
类似这样的东西:
根(字典)|StringOfViewID(Dict)-ButtonTitle(Dict)(2)-字符串字符串|ButtonTitle(Dict)(1)|细绳字符串
Root(Dict)|StringOfViewID(Dict) - ButtonTitle(Dict)(2)-String String|ButtonTitle(Dict)(1)|StringString
这是我正在使用的代码:
Here is the code I'm using:
//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];
谢谢
推荐答案
我将创建一个用作数据模型的类.这是一个简单的实现-创建Button
对象而不是传递多个参数并检索字典可能会更干净
I would create a class to serve as a data model. This is a simple implementation - it would probably be cleaner to create a Button
object rather than pass multiple parameters and retrieve a dictionary
ViewButtons.h
@interface ViewButtons : NSObject
+(ViewButtons *) viewButtonsWithContentsOfFile:(NSString *)file;
-(void) addButton:(NSString *)buttonName withX:(double) x andY:(double) y toView:(NSString *)viewName;
-(NSArray *)viewNames;
-(NSArray *)buttonNamesForView:(NSString *)viewName;
-(NSDictionary *)buttonWithName:(NSString *)name inView:(NSString *)viewName;
-(void)writeToFile:(NSString *)file;
@end
ViewButtons.m
#import "ViewButtons.h"
@interface ViewButtons ()
@property (nonatomic,strong) NSMutableDictionary *viewButtons;
@end
@implementation ViewButtons
-(id) init {
if (self=[super init]) {
self.viewButtons=[NSMutableDictionary new];
}
return self;
}
+(ViewButtons *) viewButtonsWithContentsOfFile:(NSString *)file {
ViewButtons *newViewButtons=[ViewButtons alloc];
newViewButtons.viewButtons=[NSMutableDictionary dictionaryWithContentsOfFile:file];
return newViewButtons;
}
-(void) addButton:(NSString *)buttonName withX:(double) x andY:(double) y toView:(NSString *)viewName {
NSMutableDictionary *viewDict=self.viewButtons[viewName];
if (viewDict == nil) {
viewDict=[NSMutableDictionary new];
self.viewButtons[viewName]=viewDict;
} else if (![viewDict isKindOfClass:[NSMutableDictionary class]]) {
viewDict=[viewDict mutableCopy];
self.viewButtons[viewName]=viewDict;
}
NSNumber *xNumber = [NSNumber numberWithDouble:x];
NSNumber *yNumber = [NSNumber numberWithDouble:y];
NSDictionary *buttonDict=@{@"locationX":xNumber,@"locationY":yNumber};
viewDict[buttonName]=buttonDict;
}
-(NSArray *)viewNames {
return self.viewButtons.allKeys;
}
-(NSArray *)buttonNamesForView:(NSString *)viewName {
return [self.viewButtons[viewName] allKeys];
}
-(NSDictionary *)buttonWithName:(NSString *)name inView:(NSString *)viewName {
return self.viewButtons[viewName][name];
}
-(void)writeToFile:(NSString *)file {
[self.viewButtons writeToFile:file atomically:YES];
}
@end
您可以按如下方式使用此类-
You can use this class as follows -
ViewButtons *viewButtons=[ViewButtons viewButtonsWithContentsOfFile:buttonFile];
if (viewButtons == nil) {
viewButtons=[ViewButtons new];
}
[viewButtons addButton:@"MyButton1" withX:0 andY:0 toView:@"MyView"];
[viewButtons addButton:@"MyButton2" withX:1 andY:1 toView:@"MyView"];
[viewButtons addButton:@"MyButton3" withX:0 andY:0 toView:@"MySecondView"];
[viewButtons addButton:@"MyButton4" withX:0 andY:1 toView:@"MyThirdView"];
[viewButtons writeToFile:buttonFile];
这篇关于以编程方式将字典添加到Plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!