gpt4 book ai didi

iOS 用户偏好保存和更新

转载 作者:行者123 更新时间:2023-11-29 02:03:29 25 4
gpt4 key购买 nike

我认为这个问题很难问,所以请耐心等待,如果没有意义,请告诉我。

我将首先解释情况,然后展示一些代码。所以我有一个应用程序,它有一组“默认”设置,可以显示在用户的仪表板上。用户可以修改这些设置以启用或禁用显示在其仪表板上的某些项目。

我希望能够更新“默认”设置,如果我添加新项目,它也会自动更新用户的设置,但不会覆盖他们的个人启用/禁用设置。

所以我有这个字典数组,“默认”:

dashboardItems = @[
@{
@"id" : @"1",
@"order" : @"0",
@"title" : @"Steps Traveled",
@"unit" : @"",
@"type" : HKQuantityTypeIdentifierStepCount,
@"sampleUnit" : @"count",
@"enabled" : @"1"
},
@{
@"id" : @"2",
@"order" : @"1",
@"title" : @"Flights Climbed",
@"unit" : @"",
@"type" : HKQuantityTypeIdentifierFlightsClimbed,
@"sampleUnit" : @"count",
@"enabled" : @"1"
},
@{
@"id" : @"3",
@"order" : @"2",
@"title" : @"Distance Traveled",
@"unit" : @"mi",
@"type" : HKQuantityTypeIdentifierDistanceWalkingRunning,
@"sampleUnit" : @"mi",
@"enabled" : @"1"
},
@{
@"id" : @"4",
@"order" : @"3",
@"title" : @"Active Calories",
@"unit" : @"",
@"type" : HKQuantityTypeIdentifierActiveEnergyBurned,
@"sampleUnit" : @"kcal",
@"enabled" : @"1"
},
@{
@"id" : @"5",
@"order" : @"4",
@"title" : @"Weight",
@"unit" : @"lbs",
@"type" : HKQuantityTypeIdentifierBodyMass,
@"sampleUnit" : @"lb",
@"enabled" : @"1"
},
@{
@"id" : @"6",
@"order" : @"5",
@"title" : @"Cycling",
@"unit" : @"mi",
@"type" : HKQuantityTypeIdentifierDistanceCycling,
@"sampleUnit" : @"mi",
@"enabled" : @"1"
},
@{
@"id" : @"7",
@"order" : @"6",
@"title" : @"Heart Rate",
@"unit" : @"BPM",
@"type" : HKQuantityTypeIdentifierHeartRate,
@"sampleUnit" : @"count/min",
@"enabled" : @"1"
},
@{
@"id" : @"8",
@"order" : @"7",
@"title" : @"BMI",
@"unit" : @"",
@"type" : HKQuantityTypeIdentifierBodyMassIndex,
@"sampleUnit" : @"count",
@"enabled" : @"1"
}
];

到目前为止,用户可以使用开关将启用从 1 更改为 0,反之亦然以打开或关闭该项目?

当用户注册时,应用程序会将“默认”设置保存到 NSUserDefaults 以及实时服务器中,以便在用户删除应用程序时进行备份。

如果用户注销或删除应用程序,则在下次登录时,我们会检查他们当前是否已保存 NSUserDefaults,如果没有,我们会从服务器中提取。

然后我比较这两个数组,看看是否有任何项目被更改或添加到“默认值”,这是最好的方法吗?

//here we need to check if the user does not have any preferences saved, if not pull them from Parse.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableArray *dashboardItems = [defaults objectForKey:@"dashboardItems"];

if (dashboardItems == nil) {

//user has no preferences so load them in
dashboardItems = [NSJSONSerialization JSONObjectWithData:[user[@"preferences"] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

[defaults setObject:dashboardItems forKey:@"dashboardItems"];
[defaults synchronize];

}

//compare current preferences against default to see if any new items were changed
NSArray *defaultPreferences = [[PreferencesManager sharedManager] dashboardItems];

for (NSDictionary *item in defaultPreferences) {

for (NSDictionary *userItem in dashboardItems) {

if ([item[@"id"] isEqualToString:userItem[@"id"]]) {

//we have a match, compare name and change if necessary
if (![item[@"title"] isEqualToString:userItem[@"title"]]) {

//set user's item title to default title
[userItem setValue:item[@"title"] forKey:@"title"];

}

} else {

//we did not find this in user preferences, so add it
[dashboardItems addObject:item];

}

}


}

//save defaults
[defaults setObject:dashboardItems forKey:@"dashboardItems"];
[defaults synchronize];

如果我需要进一步澄清任何事情,请告诉我。

谢谢!

最佳答案

我将从获取所有默认设置开始,然后替换那些与存储的用户设置匹配的设置:

NSMutableArray *defaultSettings = [@[] mutableCopy];
NSArray *userSettings = @[];
for (NSInteger i = 0; i < defaultSettings.count; i++) {
NSDictionary *setting = defaultSettings[i];
NSInteger index = [userSettings indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *userSetting = obj;
*stop = [setting[@"title"] isEqualToString:userSetting[@"title"]];
return *stop;
}];
if (index != NSNotFound) {
// Found matching setting
defaultSettings[i] = userSettings[index];
}
}

[defaults setObject:defaultSettings forKey:@"defaultSettings"];
[defaults synchronize];

但如果我是你,我会使用 NSDictionary 的 NSDictionary,并使用键作为 ID。这将使相同的过程变得容易得多:

NSDictionary *defaultSettings = @{
@"1": @{@"order": @"0", @"title": @"Steps traveled", @"enabled": @"0"},
@"2": @{@"order": @"1", @"title": @"Flights Climbed", @"enabled": @"0"},
@"3": @{@"order": @"2", @"title": @"Distance Traveled", @"enabled": @"0"}
};
NSMutableDictionary *mutableDefaultSettings = [defaultSettings mutableCopy];
NSDictionary *userSettings = @{
@"2": @{@"order": @"1", @"title": @"Flights Climbed", @"enabled": @"1"}
};

for (NSString *id in userSettings.allKeys) {
mutableDefaultSettings[id] = userSettings[id];
}

[defaults setObject:mutableDefaultSettings forKey:@"dashboardItems"];
[defaults synchronize];

关于iOS 用户偏好保存和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30081033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com