温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS plist文件的增 删,改查

发布时间:2020-08-07 20:19:30 来源:网络 阅读:662 作者:大头狼小鬼 栏目:移动开发

//路径

+ (NSString *)cretableName

{

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

    NSString *documentPath = [path objectAtIndex:0];

    //指定新建文件夹路径

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

    return p_w_picpathDocPath;

}


//添加

+ (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;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI