(私はIOSとObjective-Cを初めて使用するので、用語の誤用を許してください...)私のアプリでは、settings.bundleを使用してアプリの設定を保存することを選択しましたが、アプリが最初にインストールされたときとその前ユーザーがIOSデバイスの[設定]に移動すると、デフォルトは読み込まれません。私は、settings.bundleが「初期化」されるまで、アプリが独自のデフォルトを作成する必要があることを説明する多くの投稿を読みました。
これを行うために、settings.bundleを読み取り、それに応じて配列を作成するコードを作成しました。ただし、settings.bundleが初期化されていない場合は、暫定的に使用されるデフォルトの配列が作成されます。私の問題は、論理AND演算子を使用している次のコード行にあると思われます(おそらく間違っています...):
if (!([swimtypeendurance isEqualToString:@"ON"]) && !([swimtypeendurance isEqualToString:@"OFF"]))
問題は、上記のifステートメントで真の結果を得ることができないことです。私のアプリは常に「else」を実行します。私のsettings.bundleの理解は、それらが初期化されていない場合、スイッチは値がないかのようにオンにもオフにもならないということです。したがって、swimtypeenduranceの文字列がONでもOFFでもないかどうか、つまり、ONでもOFFでもないかどうかをテストしています。したがって、!を使用しようとしました。と &&...
おそらく、私が行っていることを説明するためのより良い方法は、settings.bundleから設定の1つの値をテストして、それがオンかオフかを確認することです。2つの状態のいずれかである場合は、設定バンドルが初期化されており、すべての設定を使用できると想定します。そうでない場合、つまりステートレスでない場合は、ユーザーがsettings.bundleを初期化するまで使用する配列を作成します。
質問のコンテキストを提供するためのコードの完全なスニペットは次のとおりです。
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]);
}
あなたの優れた知識と経験に基づいてコーディングに加えることができる改善を含め、これに関するヒントやコメントをいただければ幸いです。
よろしくお願いします...