2012-09-10 1 views
0

(IOS 및 Objective-C에 익숙하지 않으므로 용어 사용에 미안함 ...) 내 응용 프로그램에서 설정을 사용하도록 선택했습니다. .bundle을 사용하여 앱 설정을 저장하지만 앱을 처음 설치하고 사용자가 IOS 기기의 설정으로 이동하기 전에 기본값을로드하지 않습니다. settings.bundle이 초기화 될 때까지 앱이 자체 기본값을 만들어야한다고 설명하는 많은 게시물을 읽었습니다. settings.bundle이 초기화되기 전에 기본값을 설정하기위한 해결 방법

내가 settings.bundle에서 읽고 따라 배열을 생성하는 코드를 작성했습니다이 작업을 수행합니다. 그러나 settings.bundle이 초기화되지 않은 경우 임시로 사용되는 기본 배열이 만들어집니다.

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

문제는 내가 위의 if 문에 대한 진정한 결과를 얻을 수 없다는 것입니다 : 내 문제는 내가 논리 AND 연산자를 사용하고 다음 코드 줄 (아마 잘못 ...)와 것으로 의심 내 응용 프로그램은 항상 'else'를 실행합니다. settings.bundle에 대한 나의 이해는 초기화되지 않았다면 스위치는 값이없는 것처럼 ON 또는 OFF가되지 않는다는 것입니다. 따라서 swimtendurance에 대한 문자열이 ON 또는 OFF가 아닌지, 즉 ON이 아니고 OFF가 아닌지 확인하기 위해 테스트 중입니다. 그러므로 내 시도한! 그리고 & & ...

아마 내가하는 일을 잘 보여주는 방법은 settings.bundle에서 내 설정 중 하나의 값을 테스트하여 켜짐 또는 꺼짐을 확인하는 것입니다. 이 두 상태 중 하나 인 경우 내가 설정 번들이 초기화 된 모든 설정이없는 즉,이 비 상태 인 경우, I 사용자가 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]); 
    } 

어떤 힌트 및 의견은 대규모 I가 할 수있는 개선 사항을 포함하여 주시면 감사하겠습니다 내 당신의 뛰어난 지식을 바탕으로 코딩 경험.

+1

왜 NSUserDefaults 'registerDefaults'를 사용하지 않습니까? [SO 응답 (http://stackoverflow.com/questions/4784171/multivalue-type-settings-bundle-fields-alway-return-null/4784707#4784707). 이 방법을 사용하면 다른 것보다 훨씬 더 깨끗합니다. isEqualToString orgy –

+0

감사합니다. 이제 registerDefaults로 가서 읽을 것입니다. 왜 내 if-else가 작동하지 않는지에 대한 아이디어가 있습니까? – Sean

답변

0

확인 귀하의 도움에 미리

많은 감사 ..., 마티아스에서 주석 내 작업 솔루션으로 나를 이끌었다. 귀하의 응용 프로그램에 대한 settings.bundle이 사용자에 의해 초기화 그렇지 않은 경우 사용되는 기본값을 설정 한 경우 기본적으로 테스트를위한 일반적인 두 가지 방법이 있습니다 : 1) registerDefaults를 사용하고 2) 일부 IF-ELSE 문을 사용하여 자신 만의 테스트를 만들 수 있습니다. 질문은 두 가지 옵션 중 후자와 관련이 있으므로 registerDefaults 메서드가 아니라 대답을하겠습니다.

가장 먼저 알아야 할 사항은 boolForKey은 두 가지 조건에서 NO를 반환한다는 것입니다. 첫째, 키가 존재하지 않는 경우 (효과적으로 설정 번들이 사용자에 의해 초기화되지 않은 경우) 및로 설정된 경우 OFF 위치의 번호. 따라서 boolForKey를 사용하여 테스트하는 것은 매우 어렵습니다. 그러나 objectForKey를 사용하여 키가 있는지 여부를 테스트하면 완벽하게 작동합니다. 코드는 다음과 같습니다 (질문의 예제 코드를 참조하십시오).

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