반응형
NSLog에서 부울 플래그를 인쇄하는 방법?
NSLog에서 부울 플래그의 값을 인쇄하는 방법이 있습니까?
내가하는 방법은 다음과 같습니다.
BOOL flag = YES;
NSLog(flag ? @"Yes" : @"No");
?:
다음 형식의 삼항 조건 연산자입니다.
condition ? result_if_true : result_if_false
적절한 경우 실제 로그 문자열을 적절하게 대체하십시오.
%d
, 0 은 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
Swift에서는 할 수 있습니다.
let testBool: Bool = true
NSLog("testBool = %@", testBool.description)
이것은 기록합니다 testBool = true
이것은 Devang의 질문에 대한 직접적인 대답은 아니지만 아래 매크로는 BOOL을 기록하려는 사람들에게 매우 도움이 될 수 있다고 생각합니다. 이렇게하면 부울 값이 로그 아웃되고 변수 이름으로 자동으로 레이블이 지정됩니다.
#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의 가치를 올바르게 제공했습니다.
4 가지 방법으로 확인할 수있다
첫 번째 방법은
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
Swift에서는 단순히 부울 값을 인쇄하면 true
또는 로 표시됩니다 false
.
let flag = true
print(flag) //true
이를 수행하는 방법은 다음과 같습니다.
BOOL flag = NO;
NSLog(flag ? @"YES" : @"NO");
//assuming b is BOOL. ternary operator helps us in any language.
NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));
참고 URL : https://stackoverflow.com/questions/6358349/how-to-print-boolean-flag-in-nslog
반응형
'development' 카테고리의 다른 글
힘내-현재 분기 바로 가기를 밀어 (0) | 2020.03.15 |
---|---|
CSS에서만 위첨자? (0) | 2020.03.15 |
리포지토리 액세스가 거부되었습니다. (0) | 2020.03.15 |
std :: string을 char *로 (0) | 2020.03.15 |
RecyclerView로 수평 ListView를 작성하는 방법은 무엇입니까? (0) | 2020.03.15 |