Excel生成plist文件

前言

写代码也已经好久了,虽然没少接触plist,但是还从来没有自己生成过plist文件.
今天就来制作一个plist文件

Excel生成plist文件

首先先将我们要用的数据写入excel表哥.如图:

在这里插入图片描述

将制作的表格转化为csv文件,使其转换为以逗号(,)分隔开的数据,以文本编辑的方法打开
如图:

在这里插入图片描述

在新建一个文本编辑使用纯文本模式,最好保存的时候生成txt后缀.

打开的文本编辑制作为纯文本模式或者使用快捷键
shift+command+t
保存成txt格式的文件
如图:
在这里插入图片描述
这里为什么要这样转换呢?因为其他形式转换容易在换行的时候生成\或者空格\最后多出来一个空行,在转换的时候会出现错误.
如图:

在这里插入图片描述

错误如下:
在这里插入图片描述

将生成好的txt文件拖入工程中,直接上代码生成就好

-(void)generatePlist
{
	NSString *path = [[NSBundle mainBundle]pathForResource:@"color" ofType:@"txt"];
    NSStringEncoding gbk = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString *content = [[NSString alloc]initWithContentsOfFile:path encoding:gbk error:nil];
    NSArray *arrat = [content componentsSeparatedByString:@"\r\n"];
    NSString *docuPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *plistPath = [docuPath stringByAppendingPathComponent:@"ceshiSDKS.plist"];
    NSLog(@"==%@",plistPath);
    NSMutableArray *resulrArr = [NSMutableArray array];
    for (NSInteger j = 0; j < arrat.count; j++) {
        NSString* currentContent = [arrat objectAtIndex:j];
        NSArray *timeDataArr = [currentContent componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setObject:[timeDataArr objectAtIndex:0] forKey:@"colorName"];
        [dic setObject:[timeDataArr objectAtIndex:1] forKey:@"isSelected"];
        [resulrArr addObject:dic];
    }
    [resulrArr writeToFile:plistPath atomically:YES];
}

这样就将生成的plist保存在了沙盒里.
进入沙盒查看

window->Devices and Simulators->选择项目工程后点设置图标->Download Container

将文件保存在桌面,打开->显示包内容
在这里插入图片描述
打开查看:
在这里插入图片描述

这样我们需求的plist文件就生成好了.

猜你喜欢

转载自blog.csdn.net/Q_Li_Mountain/article/details/108529863
今日推荐