如何在info.plist文件中存取数据
1个回答
2017-01-07 · 知道合伙人软件行家
关注
展开全部
这有一个读取plist文件的例子,读取基本上每个项目都有的info.plist文件。这个例子是在一个splash view中显示程序的版本号(对应info.plist中的CFBundleVersion)。
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent: @"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,100,60,25)];
// for example
versionLabel.backgroundColor = [UIColor clearColor];
versionLabel.textColor = [UIColor whiteColor];
versionLabel.font = [UIFont systemFontOfSize:10];
NSString *versionString = [NSString stringWithFormat:@"v%@", [plistData objectForKey: @"CFBundleVersion"]];
versionLabel.text = versionString;
[self.view addSubview:versionLabel];
注意上面加粗体的部分,可以看到程序是如何读取dictionary类型的数据,从key得到value的。
plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法:以下代码在Mac和iPhone中均适用。
写入plist文件:
NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ]initWithContentsOfFile: @"/Sample.plist" ];
[ dict setObject:@"Yes" forKey:@"RestartSpringBoard" ];
[ dict writeToFile:@"/Sample.plist" atomically:YES ];
读取plist文件:
NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile: @"/Sample.plist" ];
NSString* object = [ dict objectForKey:@"RestartSpringBoard" ];
//读plist 文件 到 NSMutableDictionary 中
dictplist = [[NSMutableDictionary alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
[dictplist setObject:@"testOne" forKey:@"key1"];
NSLog([dictplist objectForKey:@"key1"]);
NSArray *array = [[NSArray alloc] initWithObjects: @"item1",@"item2", @"item3",nil];
[dictplist setObject:array forKey:@"arrayitemtest"];
[dictplist writeToFile:plistPath atomically:YES];
NSString *name = [dictplist objectForKey:@"name" ];
NSLog(name);
arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"arrayitemtest"]];
//从NSMutableDictionary 中构建 plist 文件
NSArray *array = [[NSArray alloc] initWithObjects: @"item1",@"item2", @"item3",nil];
dictplist = [[NSMutableDictionary alloc ] init];
[dictplist setObject:@"nameOne" forKey:@"name"];
[dictplist setObject:array forKey:@"item"];
[dictplist writeToFile:plistPath atomically:YES];
//arraylist = [[NSMutableArray alloc] init];
//[arraylist addObject:[dictplist objectForKey:@"name"]];
arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"item"]];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent: @"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,100,60,25)];
// for example
versionLabel.backgroundColor = [UIColor clearColor];
versionLabel.textColor = [UIColor whiteColor];
versionLabel.font = [UIFont systemFontOfSize:10];
NSString *versionString = [NSString stringWithFormat:@"v%@", [plistData objectForKey: @"CFBundleVersion"]];
versionLabel.text = versionString;
[self.view addSubview:versionLabel];
注意上面加粗体的部分,可以看到程序是如何读取dictionary类型的数据,从key得到value的。
plist文件是标准的xml文件,在cocoa中可以很简单地使用。这里介绍一下使用方法:以下代码在Mac和iPhone中均适用。
写入plist文件:
NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ]initWithContentsOfFile: @"/Sample.plist" ];
[ dict setObject:@"Yes" forKey:@"RestartSpringBoard" ];
[ dict writeToFile:@"/Sample.plist" atomically:YES ];
读取plist文件:
NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile: @"/Sample.plist" ];
NSString* object = [ dict objectForKey:@"RestartSpringBoard" ];
//读plist 文件 到 NSMutableDictionary 中
dictplist = [[NSMutableDictionary alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
[dictplist setObject:@"testOne" forKey:@"key1"];
NSLog([dictplist objectForKey:@"key1"]);
NSArray *array = [[NSArray alloc] initWithObjects: @"item1",@"item2", @"item3",nil];
[dictplist setObject:array forKey:@"arrayitemtest"];
[dictplist writeToFile:plistPath atomically:YES];
NSString *name = [dictplist objectForKey:@"name" ];
NSLog(name);
arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"arrayitemtest"]];
//从NSMutableDictionary 中构建 plist 文件
NSArray *array = [[NSArray alloc] initWithObjects: @"item1",@"item2", @"item3",nil];
dictplist = [[NSMutableDictionary alloc ] init];
[dictplist setObject:@"nameOne" forKey:@"name"];
[dictplist setObject:array forKey:@"item"];
[dictplist writeToFile:plistPath atomically:YES];
//arraylist = [[NSMutableArray alloc] init];
//[arraylist addObject:[dictplist objectForKey:@"name"]];
arraylist = [[NSMutableArray alloc] initWithArray:[dictplist objectForKey:@"item"]];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询