用NSUserDefaults保存app用户可变数据

#define kUserDefaults       [NSUserDefaults standardUserDefaults]
- (void)viewDidLoad
{
    [super viewDidLoad];

    WS(weakSelf)
    [LoginHttps fetchNewGuidesImages:weakSelf.navigationController.view success:^(NSArray *data) {
        //1.获取旧值
          //1.1注意第一次要给它分配空间 (不然idSetStr为nil, 调用appendFormat后 会一直为nil,导致UserDefault无法保存最新值)
        NSMutableString *idSetStr = [NSMutableString stringWithCapacity:0];
        if ([kUserDefaults objectForKey:kNewGuidePictureID]) {
            idSetStr = [NSMutableString stringWithFormat:@"%@", [kUserDefaults objectForKey:kNewGuidePictureID]];
        }

        //....
        //2.保存新值
        if(needSave)
        {
            for (LaunchModel *model in data) {
                //1.2 确保idSetStr不为nil
                [idSetStr appendFormat:@"%ld,",model.ID];
            }
            [kUserDefaults setObject:idSetStr forKey:kNewGuidePictureID];

        }

    } failure:^{

    }];
}

你可能感兴趣的:(用NSUserDefaults保存app用户可变数据)