Plist文件的读写操作

/**

 *  写入plist文件

 *

 *  @param writecontext 要写入的内容

 *  @param writeName    要写入内容的对应键

 */

+(void)writeToPlist:(ZMPlistKey)zmPlistKey name:(NSString *)writecontext

{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *path =[paths objectAtIndex:0];

    NSString *filename = [path stringByAppendingPathComponent:kPlistName];   //获取路径

    

    //创建一个dic,写到plist文件

    NSString *writeName = [self returnPlistKey:zmPlistKey];

    

    if (writeName) {

        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@",writecontext],writeName,nil]; //写入数据

        

        [dic writeToFile:filename atomically:YES];

        

        [[NSNotificationCenter defaultCenter] postNotificationName:@"token" object:nil];

    }

}


/**

 *  读取plist文件

 *

 *  @param key 对应的键

 *

 *  @return 对应键的内容

 */

+(NSString *)readToPlist:(ZMPlistKey)zmPlistKey

{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *path = [paths objectAtIndex:0];

    NSString *filename = [path stringByAppendingPathComponent:kPlistName];   //获取路径

    

    NSLog(@"%@",filename);

    

    NSString *readName = [self returnPlistKey:zmPlistKey];

    NSString *str = nil;

    

    if (readName) {

        NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];  //读取数据

        str = dic2[readName];

    }

    

    return str;

}


/**

 *  dict写入plist文件

 *

 *  @param dict 要写入plist文件的dict

 */

+(void)dictWriteToPlist:(NSDictionary *)dict plist:(NSString *)plist

{

    NSString *error;

    

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

                                                              NSUserDomainMask, YES) objectAtIndex:0];

    

    NSString *plistPath = [rootPath stringByAppendingPathComponent:plist];

    

    NSLog(@"write %@",plistPath);

    

    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dict

                         

                                                                   format:NSPropertyListXMLFormat_v1_0

                         

                                                         errorDescription:&error];

    

    [plistData writeToFile:plistPath atomically:YES];

}


/**

 *  读取dictplist文件

 *

 *  @param plist 读取plist文件

 */

+(NSDictionary *)dictReadPlist:(NSString *)plist

{

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

                                                              NSUserDomainMask, YES) objectAtIndex:0];

    

    NSString *plistPath = [rootPath stringByAppendingPathComponent:plist];

    

    NSLog(@"read %@",plistPath);

    

    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

    

    return dictionary;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值