有办法打印值布尔标志在NSLog?
当前回答
虽然这不是对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
其他回答
%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
我是这样做的:
BOOL flag = YES;
NSLog(flag ? @"Yes" : @"No");
?:是三元条件运算符,形式为:
condition ? result_if_true : result_if_false
在适当的地方替换实际的日志字符串。
NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership); // prints 1 or 0
虽然这不是对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"));
推荐文章
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像