ios Objective-C 2.3 布尔Boolean类型

java与c语言中的bool类型有true和false两种两个值,bool是用来存储真/假值的类型。

1.Object-C也提供了一个类似的BOOL类型,它又YES和NO两个值。
2.BOOL和bool在同一个程序中可以共同存在,但是如果是Cocoa代码则只能用BOOL。

1.BOOL

它是对带符号的字符类型的类型定义,用8位的存储空间,通过宏定义

#define    YES = 1
#define    NO = 0

如果将大于8位的整型值赋值给BOOL类型的变量,一定要小心,它只会去后8位即低位的一个字节

如8960十六进制为0x2300,则转换为BOOL值为0认做是NO值。


//
//  main.m
//  Hello Project-c
//
//  Created by Bin Wang on 2018/6/4.
//  Copyright © 2018年 Isan. All rights reserved.
//

#import 

BOOL  areIntsDifferent(int var1,int var2){
    
    if(var1 == var2){
        
        return NO;
        
    }else{
        return YES;
    }
}

//返回值是指向NSString的指针,用%@来输出
NSString  *boolString(BOOL yesNo){
    
    if(yesNo == NO){
        
        return (@"NO");
    }else{
        return (@"YES");
    }
    
}


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
    }
    
    NSLog(@"Hello,Objective-C !");
    
    BOOL areTheyDifferent;
    areTheyDifferent = areIntsDifferent(10, 24);
    
    NSLog(@ "are %d and %d  different?  %@",10,24,boolString(areTheyDifferent));
    
    areTheyDifferent = areIntsDifferent(15, 15);
    
    NSLog(@ "are  %d and %d different?  %@",15,15,boolString(areTheyDifferent));
    
    return 0;
}


运行结果:

2018-06-20 01:01:55.945924+0800 Hello Project-c[843:293486] Hello, World!
2018-06-20 01:01:55.946146+0800 Hello Project-c[843:293486] Hello,Objective-C !
2018-06-20 01:01:55.946199+0800 Hello Project-c[843:293486] are 10 and 24  different?  YES
2018-06-20 01:01:55.946223+0800 Hello Project-c[843:293486] are  15 and 15 different?  NO
Program ended with exit code: 0

你可能感兴趣的:(ios Objective-C 2.3 布尔Boolean类型)