iOS plist文件的增 删,改查

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS plist文件的增 删,改查相关的知识,希望对你有一定的参考价值。

//路径

+ (NSString *)cretableName

{

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

    NSString *documentPath = [path objectAtIndex:0];

    //指定新建文件夹路径

    NSString *imageDocPath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"setPlace.plist"]];

    return imageDocPath;

}


//添加

+ (void)addPlaceWrite:(NSDictionary *)dict anCity:(NSString *)cityName

{

  NSString *filePath = [self cretableName];

    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

    if(nil == dic)

    {

        dic = [NSMutableDictionary dictionaryWithCapacity:0];

    }

    NSMutableArray *dataArry = [dic valueForKey:cityName];

    if(nil == dataArry)

    {

        dataArry = [NSMutableArray arrayWithCapacity:0];

    }

    if (0  == dataArry.count)

    {

        [dataArry addObject:dict];

    }

    else

    {

        [dataArry addObject:dict];

    }

    [dic setValue:dataArry forKey:cityName];

    [dic writeToFile:filePath atomically:YES];

}


//所有

+ (NSArray *)allPlaceData:(NSString *)cityName

{

    NSString *filePath = [self cretableName];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

    NSArray *aray = dict[cityName];

    return aray;

}


//删除

+ (void)deletePlaceData:(NSInteger)index anCity:(NSString *)cityName

{

    NSString *filePath = [self cretableName];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

    NSMutableArray *arry = dict[cityName];

    [arry removeObjectAtIndex:index];

    [dict setValue:arry forKey:cityName];

    [dict writeToFile:filePath atomically:YES];

}

//修改

+ (void)modifyPlaceData:(NSDictionary *)dic anPlace:(NSInteger)index anCity:(NSString *)cityName

{

    NSString *filePath = [self cretableName];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

    NSMutableArray *arry = dict[cityName];

     [arry removeObjectAtIndex:index];

    [arry insertObject:dic atIndex:index];

    [dict setValue:arry forKey:cityName];

    [dict writeToFile:filePath atomically:YES];

}


//查询

+ (BOOL)allCity:(NSString *)anCity

{

    BOOL  isBlean = NO;

    NSString *filePath = [self cretableCityName];

    NSArray *arry = [NSArray arrayWithContentsOfFile:filePath];

    for(NSString *city in arry)

    {

      if([anCity isEqualToString:city])

      {

          isBlean = YES;

          break;

      }

    }

    return isBlean;

}


本文出自 “11204872” 博客,请务必保留此出处http://11214872.blog.51cto.com/11204872/1754481

以上是关于iOS plist文件的增 删,改查的主要内容,如果未能解决你的问题,请参考以下文章

IOS:检查 plist 文件

iOS开发之JSON转PLIST(把存储json格式的文件转换成plist文件)

在 iOS 上运行的 plist 文件出现问题:Info.plist:在第 44 行遇到意外元素(plist 只能包含一个对象)

IOS 读取项目中的plist文件

iOS,plist文件pch文件

加密/解密 .plist 文件 ios