
2. 新建一个plist文件(command+N快捷键或者新建文件 如图下的红色圈)
3. 红色圈是plist的文件名,这里默认为Property List,点击创建就可以了
4.plist文件如下所示
5.如图所示类型为字典类型
6.两个字典创建好了,注意这里给字典里添加数据时,要点击teacher的三角符号朝下时,点击“+”,这样才创建在字典里,不按照这样做,会在字典外面。
7.添加数据
8.plistde的文件创建好了,创建一个新的文件
1创建.plist文件。
方法:
1、选中Xcode,File-New-File…;
2、在工程目录处右键,New File…;
3、选中Xcode,使用快捷方式cmd+n组合键;
4、代码创建
可以选择iOS和OS X中任意一个“Resource”中的“Property List”创建并命名保存。当前名称为:usersList.plist。
2
往.plist文件添加数据并显示
在Xcode项目中点击usersList.plist,可以打开显示usersList.plist文件中的内容。
可以在空白处右键“Add Row”添加数据或者点中“root”行,点击“+”号即可添加。一般数据类型(Type)可以选择Array、Dictionary、String等类型。同时也可以给添加的数据赋值(Value)。
除了以Property List列表形式显示外,选中usersList.plist文件,右键“Open As”-“Source Code”可以以XML格式显示。当前 *** 作结果如图。
多图
3
使用代码创建.plist文件。
//获取本地沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
//获取完整路径
NSString *documentsPath = [path objectAtIndex:0]
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"usersList.plist"]
NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init]
//设置属性值
[usersDic setObject:@"chan" forKey:@"name"]
[usersDic setObject:@"123456" forKey:@"password"]
主要 *** 作:1.//获得plist路径-(NSString*)getPlistPath;
2.//判断沙盒中名为plistname的文件是否存在-(BOOL) isPlistFileExists;
3.//读取沙盒中Document文件夹下的BookList.plist文件
[NSMutableDictionarydictionaryWithContentsOfFile:plistPath]
4.//写入文件 if ([plistDictionary writeToFile:plistPath atomically:YES])
WBBooksManager.m文件:
#import "WBBooksManager.h"
@implementation WBBooksManager
static WBBooksManager *g_instance = nil
+ (WBBooksManager *)sharedInstance
{
@synchronized(self) {
if ( g_instance == nil ) {
g_instance = [[self alloc] init]
}
}
return g_instance
}
//获得plist路径
-(NSString*)getPlistPath{
//沙盒中的文件路径
NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *doucumentsDirectiory = [storeFilePath objectAtIndex:0]
NSString *plistPath =[doucumentsDirectiory stringByAppendingPathComponent:@"WBBooks.plist"] //根据需要更改文件名
return plistPath
}
//判断沙盒中名为plistname的文件是否存在
-(BOOL) isPlistFileExists{
NSString *plistPath =[[WBBooksManager sharedInstance]getPlistPath]
NSFileManager *fileManager = [NSFileManager defaultManager]
if( [fileManager fileExistsAtPath:plistPath]== NO ) {
NSLog(@"not exists")
return NO
}else{
return YES
}
}
-(void)initPlist{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath]
//如果plist文件不存在,将工程中已建起的plist文件写入沙盒中
if (! [[WBBooksManager sharedInstance] isPlistFileExists]) {
//从自己建立的plist文件 复制到沙盒中 ,方法一
NSError *error
NSFileManager *fileManager = [NSFileManager defaultManager]
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"WBBooks" ofType:@"plist"]
[fileManager copyItemAtPath:bundle toPath:plistPath error:&error]
//方法二
//NSString *path = [[NSBundle mainBundle] pathForResource:@"WBBooks"ofType:@"plist"]
//NSMutableDictionary *activityDics = [[NSMutableDictionary alloc] initWithContentsOfFile:path]
//[activityDics writeToFile:plistPath atomically:YES]
}
}
//判断key的书是否存在
-(BOOL)isBookExistsForKey:(NSString*)key{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath]
NSMutableDictionary *WBBooksDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]
//根目录下存在名为bookname的字典
if ([WBBooksDictionary objectForKey:key]) {
return YES
}else{
return NO
}
}
//根据key值删除对应书籍
-(void)removeBookWithKey:(NSString *)key{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath]
NSMutableDictionary *WBBooksDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]
[WBBooksDictionary removeObjectForKey:key]
[WBBooksDictionary writeToFile:plistPath atomically:YES]//删除后重新写入
}
//删除plistPath路径对应的文件
-(void)deletePlist{
NSFileManager *fileManager = [NSFileManager defaultManager]
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath]
[fileManager removeItemAtPath:plistPath error:nil]
}
//将dictionary写入plist文件,前提:dictionary已经准备好
-(void)writePlist:(NSMutableDictionary*)dictionary forKey:(NSString *)key{
NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc]init]
//如果已存在则读取现有数据
if ([[WBBooksManager sharedInstance]isPlistFileExists]) {
plistDictionary = [[WBBooksManager sharedInstance]readPlist]
}
//增加一个数据
[plistDictionary setValue:dictionary forKey:key]//在plistDictionary增加一个key为...的value
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath]
if([plistDictionary writeToFile:plistPath atomically:YES]){
NSLog(@"write ok!")
}else{
NSLog(@"ddd")
}
}
//
-(NSMutableDictionary*)readPlist{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath]
NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath]
return resultDictionary
}
//读取plist文件内容复制给dictionary 备用
-(void)readPlist:(NSMutableDictionary **)dictionary{
NSString *plistPath = [[WBBooksManager sharedInstance] getPlistPath]
*dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]
}
//更改一条数据,就是把dictionary内key重写
-(void)replaceDictionary:(NSMutableDictionary *)newDictionary withDictionaryKey:(NSString *)key{
[[WBBooksManager sharedInstance]removeBookWithKey:key]
[[WBBooksManager sharedInstance]writePlist:newDictionary forKey:key]
}
-(NSInteger)getBooksCount{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]
dictionary = [[WBBooksManager sharedInstance] readPlist]
return [dictionary count]
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)