溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

iOS plist文件的增 刪,改查

發(fā)布時(shí)間:2020-08-07 20:19:30 來(lái)源:網(wǎng)絡(luò) 閱讀:660 作者:大頭狼小鬼 欄目:移動(dòng)開(kāi)發(fā)

//路徑

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

}


//查詢(xún)

+ (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問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI