加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 我们如何在Xcode项目中创建自己的plist文件?

发布时间:2020-12-15 01:47:35 所属栏目:百科 来源:网络整理
导读:我想在我的应用程序中创建“UrlPaths.plist”文件,还有一个包含4或5个对象的字典.请帮我创建一个plist文件和字典.并且还从该plist文件中读取数据. 我希望plist将文件添加到资源文件夹,我想在那时添加词典.我不想要pragmatical创建plist但我想要实际阅读数据.
我想在我的应用程序中创建“UrlPaths.plist”文件,还有一个包含4或5个对象的字典.请帮我创建一个plist文件和字典.并且还从该plist文件中读取数据.

我希望plist将文件添加到资源文件夹,我想在那时添加词典.我不想要pragmatical创建plist但我想要实际阅读数据.

解决方法

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) {
            data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
}
else {
    // If the file doesn’t exist,create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

//To insert the data into the plist
data[@"value"] = @(5);
[data writeToFile: path atomically:YES];
[data release];

//To retrieve the data from the plist
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
int value1;
value1 = [savedStock[@"value"] intValue];
NSLog(@"%i",value1);
[savedStock release];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读