NSLog에서 부울 플래그를 인쇄하는 방법은 무엇입니까?
NSLog에서 Boolean 플래그의 값을 인쇄하는 방법이 있습니까?
방법은 다음과 같습니다.
BOOL flag = YES;
NSLog(flag ? @"Yes" : @"No");
?:는 다음 형식의 3차 조건 연산자입니다.
condition ? result_if_true : result_if_false
적절한 경우 실제 로그 문자열을 대체합니다.
%d0은 FALSE, 1은 TRUE입니다.
BOOL b;
NSLog(@"Bool value: %d",b);
또는
NSLog(@"bool %s", b ? "true" : "false");
데이터 유형을 기준%@다음과 같은 변화
For Strings you use %@
For int you use %i
For float and double you use %f
불리언은 정수에 불과합니다. 그냥 형식적으로 주조된 값입니다.
typedef signed char BOOL;
#define YES (BOOL)1
#define NO (BOOL)0
BOOL value = YES;
NSLog(@"Bool value: %d",value);
출력이 1이면 YES 그렇지 않으면 NO
스위프트에서는 다음을 수행할 수 있습니다.
let testBool: Bool = true
NSLog("testBool = %@", testBool.description)
로그에 기록됩니다.testBool = true
이것이 데방의 질문에 대한 직접적인 대답은 아니지만, 저는 아래 매크로가 BOOLs를 기록하려는 사람들에게 매우 도움이 될 수 있다고 생각합니다.그러면 부울 값이 로그아웃되고 변수 이름으로 자동으로 레이블이 지정됩니다.
#define LogBool(BOOLVARIABLE) NSLog(@"%s: %@",#BOOLVARIABLE, BOOLVARIABLE ? @"YES" : @"NO" )
BOOL success = NO;
LogBool(success); // Prints out 'success: NO' to the console
success = YES;
LogBool(success); // Prints out 'success: YES' to the console
Apple의 FixIt는 %hhd를 제공했으며, 이는 내 BOOL의 가치를 정확하게 제공했습니다.
네 가지 방법으로 확인할 수 있습니다.
첫번째 방법은
BOOL flagWayOne = TRUE;
NSLog(@"The flagWayOne result is - %@",flagWayOne ? @"TRUE":@"FALSE");
두번째 방법은
BOOL flagWayTwo = YES;
NSLog(@"The flagWayTwo result is - %@",flagWayTwo ? @"YES":@"NO");
세번째 방법은
BOOL flagWayThree = 1;
NSLog(@"The flagWayThree result is - %d",flagWayThree ? 1:0);
네번째 방법은
BOOL flagWayFour = FALSE; // You can set YES or NO here.Because TRUE = YES,FALSE = NO and also 1 is equal to YES,TRUE and 0 is equal to FALSE,NO whatever you want set here.
NSLog(@"The flagWayFour result is - %s",flagWayFour ? YES:NO);
NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership); // prints 1 or 0
다음은 이를 수행하는 방법입니다.
BOOL flag = NO;
NSLog(flag ? @"YES" : @"NO");
Swift에서는 부울 값을 인쇄하기만 하면 다음과 같이 표시됩니다.true또는false.
let flag = true
print(flag) //true
//assuming b is BOOL. ternary operator helps us in any language.
NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));
- 정수로 직접 인쇄 부울
BOOL curBool = FALSE;
NSLog(@"curBool=%d", curBool);
->curBool=0
- 부울을 문자열로 변환
char* boolToStr(bool curBool){
return curBool ? "True": "False";
}
BOOL curBool = FALSE;
NSLog(@"curBool=%s", boolToStr(curBool));
->curBool=False
언급URL : https://stackoverflow.com/questions/6358349/how-to-print-boolean-flag-in-nslog
'source' 카테고리의 다른 글
| Xcode 4에서 "기존 프레임워크를 추가"하려면 어떻게 해야 합니까? (0) | 2023.05.20 |
|---|---|
| res.end()와 res.send()의 차이점은 무엇입니까? (0) | 2023.05.20 |
| jQuery Event : div의 html/text 변경사항 탐지 (0) | 2023.05.20 |
| Office Open XML Cell에 날짜/시간 값이 포함되어 있음을 나타내는 것은 무엇입니까? (0) | 2023.05.20 |
| Git에서 손실된 저장소를 복구하려면 어떻게 해야 합니까? (0) | 2023.05.20 |