有办法打印值布尔标志在NSLog?
我是这样做的:
BOOL flag = YES;
NSLog(flag ? @"Yes" : @"No");
?:是三元条件运算符,形式为:
condition ? result_if_true : result_if_false
在适当的地方替换实际的日志字符串。
%d, 0为假,1为真。
BOOL b;
NSLog(@"Bool value: %d",b);
or
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
虽然这不是对Devang的问题的直接回答,但我相信下面的宏对寻找日志bool的人非常有帮助。这将注销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
//assuming b is BOOL. ternary operator helps us in any language.
NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));
NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership); // prints 1 or 0
注意,在Swift中,你可以这样做
let testBool: Bool = true
NSLog("testBool = %@", testBool.description)
这将记录testBool = true
我们可以用四种方法检查
第一种方法是
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);
直接将bool值打印为整数
BOOL curBool = FALSE;
NSLog(@"curBool=%d", curBool);
- > curBool = 0
将bool类型转换为字符串
char* boolToStr(bool curBool){
return curBool ? "True": "False";
}
BOOL curBool = FALSE;
NSLog(@"curBool=%s", boolToStr(curBool));
- > curBool =虚假
推荐文章
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- Xcode 4挂在“附加到(应用程序名称)”
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue
- 在Objective-C中@synchronized如何锁定/解锁?
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?