有办法打印值布尔标志在NSLog?


当前回答

在Swift中,你可以简单地打印一个布尔值,它将显示为true或false。

let flag = true
print(flag) //true

其他回答

注意,在Swift中,你可以这样做

let testBool: Bool = true
NSLog("testBool = %@", testBool.description)

这将记录testBool = true

NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership);  // prints 1 or 0

苹果的FixIt提供了%hhd,它正确地给了我BOOL的值。

%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 = NO;
NSLog(flag ? @"YES" : @"NO");