简体   繁体   English

在初始化settings.bundle之前设置默认值的解决方法

[英]Work-around for setting defaults before settings.bundle is initialised

(I'm new to IOS and Objective-C so please excuse any miss-use of terminology...) In my app I have chosen to use the settings.bundle to store my app settings but when the app is first installed and before the user navigates to Settings on their IOS device it does not load the defaults. (我是IOS和Objective-C的新手,所以请宽恕术语的任何误用...)在我的应用程序中,我选择使用settings.bundle来存储我的应用程序设置,但是该应用程序是在首次安装和安装之前用户导航至其IOS设备上的“设置”,则不会加载默认值。 I have read many posts explaining that the app should create its own defaults until the settings.bundle has been 'initialised'. 我读过许多文章,解释了该应用程序应创建自己的默认值,直到settings.bundle被“初始化”为止。

To do this I have written some code that reads in the settings.bundle and creates an array accordingly. 为此,我编写了一些读取settings.bundle中的代码,并相应地创建了一个数组。 But if settings.bundle has not been initialised it creates a default array to be used in the interim. 但是,如果尚未初始化settings.bundle,它将创建一个默认数组,以在过渡期间使用。 I suspect that my problem is with the following line of code where I am using a logical AND operator (probably incorrectly...): 我怀疑我的问题出在我使用逻辑AND运算符的以下代码行中(可能不正确...):

if (!([swimtypeendurance isEqualToString:@"ON"]) && !([swimtypeendurance isEqualToString:@"OFF"]))

The problem is that I cannot get a true result for the if statement above, my app always executes the 'else'. 问题是我无法获得上述if语句的真实结果,我的应用始终执行“ else”。 My understanding of settings.bundle is that if they have not been initialised then the switch is neither ON or OFF as if it has no value. 我对settings.bundle的理解是,如果尚未初始化它们,那么开关就不会像没有任何值一样在ON或OFF位置。 I am therefore testing to see if the string for swimtypeendurance is neither ON or OFF, in other words if it is not ON AND it is not OFF - hence my attempted use of the ! 因此,我正在测试swimtypeendurance的字符串是ON还是OFF,换句话说,如果不是ON且不是OFF,则尝试使用! and &&... 和&& ...

Perhaps a better way to exaplain what I am doing is: I am testing the value of one of my settings from settings.bundle to see if it is either on or off. 检查我在做什么的一种更好的方法是:我正在settings.bundle中测试我的一个设置的值,以查看它是打开还是关闭。 If it is one of the two states then I assume that the settings bundle has been initialised and all settings can be used, if not ie it is stateless then I create an array to be used until the user does initialise settings.bundle. 如果它是两种状态之一,那么我假设设置包已被初始化并且可以使用所有设置;如果不是,那就是无状态的,然后我创建一个要使用的数组,直到用户初始化settings.bundle为止。

Here is the complete snippet of code to provide context to the question: 这是为问题提供上下文的完整代码段:

NSMutableArray* arrayofswimtypes = [NSMutableArray arrayWithCapacity:15];
    NSString *swimtt750 = [defaults boolForKey:kswimtypett750mKey]? @"ON" : @"OFF";
    if ([swimtt750 isEqualToString:@"ON"]) {
        swimTypeTt750m = [[NSString alloc] initWithFormat:@"Time Trial - 750m"];
        [arrayofswimtypes addObject:[[NSString alloc] initWithFormat:@"%@", swimTypeTt750m]];
    }
    NSString *swimtt1500 = [defaults boolForKey:kswimtypett1500mKey]? @"ON" : @"OFF";
    if ([swimtt1500 isEqualToString:@"ON"]) {
        swimTypeTt1500m = [[NSString alloc] initWithFormat:@"Time Trial - 1500m"];
        [arrayofswimtypes addObject:[[NSString alloc] initWithFormat:@"%@", swimTypeTt1500m]];
    }

    [arrayofswimtypes addObject:[[NSString alloc] initWithFormat:@"General Session"]];

    NSString *swimtypedrills = [defaults boolForKey:kswimtypedrillsKey]? @"ON" : @"OFF";
    if ([swimtypedrills isEqualToString:@"ON"]) {
        swimTypeDrills = [[NSString alloc] initWithFormat:@"Drills Session"];
        [arrayofswimtypes addObject:[[NSString alloc] initWithFormat:@"%@", swimTypeDrills]];
    }
    NSString *swimtypeendurance = [defaults boolForKey:kswimtypeenduranceKey]? @"ON" : @"OFF";
    if ([swimtypeendurance isEqualToString:@"ON"]) {
        swimTypeEndurance = [[NSString alloc] initWithFormat:@"Endurance Session"];
        [arrayofswimtypes addObject:[[NSString alloc] initWithFormat:@"%@", swimTypeEndurance]];
    }

    //Place an if-else statement in here to load a default array if the user has not been to settings and initialised the settings.bundle
    if (!([swimtypeendurance isEqualToString:@"ON"]) && !([swimtypeendurance isEqualToString:@"OFF"])) {
        swimSessTypesArray = [[NSMutableArray alloc] initWithObjects:[[NSString alloc] initWithFormat:@"Time Trial - 750m"],
                              [[NSString alloc] initWithFormat:@"Time Trial - 1500m"], 
                              [[NSString alloc] initWithFormat:@"General Session"],
                              [[NSString alloc] initWithFormat:@"Drills Session"],
                              [[NSString alloc] initWithFormat:@"Endurance Session"], nil];
        NSLog(@"Count of swimSessTypesArray NOT FROM Defaults: %i", [swimSessTypesArray count]);
    } else {
        swimSessTypesArray = arrayofswimtypes;
        NSLog(@"Count of swimSessTypesArray from Defaults: %i", [swimSessTypesArray count]);
    }

Any hints and comments on this would be massively appreciated including any improvements that I can make to my coding based on your superior knowledge and experience. 我们将不胜感激对此的任何提示和评论,包括根据您的专业知识和经验,我可以对编码进行的任何改进。

Many thanks in advance for your assistance... 预先非常感谢您的协助...

OK, the comment from Matthias has led me to my working solution. 好的,Matthias的评论使我找到了可行的解决方案。 Essentially there are two common methods for testing if the settings.bundle for your app have been initialised by the user and set defaults to be used if not: 1) use registerDefaults and 2) create your own tests by using some IF-ELSE statements. 基本上,有两种通用的测试方法,用于测试用户是否已初始化应用程序的settings.bundle并设置了默认设置(如果没有使用):1)使用registerDefaults和2)使用某些IF-ELSE语句创建自己的测试。 Since the question is related to the latter of the two options I will answer it and not the registerDefaults method. 由于问题与两个选项中的后者有关,因此我将回答它,而不是registerDefaults方法。

The first thing that one should know is that boolForKey will return a NO under 2 different conditions: firstly when the key does not exist (effectively when the settings bundle has not been initialised by the user) and when it is set to the no of OFF position. 首先要知道的是boolForKey将在2种不同的条件下返回NO:首先,当该键不存在时(有效地是当用户未初始化设置包时),以及将其设置为OFF时,位置。 This makes it very impossible to test using boolForKey but if you use objectForKey to test whether or not the key exists then it works perfectly. 这使得使用boolForKey进行测试非常不可能,但是如果您使用objectForKey来测试密钥是否存在,那么它将完美地工作。 The code looks like this (in keeping with the example code from the question): 代码看起来像这样(与问题中的示例代码保持一致):

if (![boolForKey:kswimtypeednurancekey]) {
Create your default settings to be used by the app;
} else {
Use the settings from the settings.bundle;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM