首页 / 操作系统 / Linux / Objective-C学习——布尔类型
参照书中关于布尔类型实例,敲出下面代码
#import<Foundation/Foundation.h>//比较两个整数是否相等
BOOL areIntsDifferent(int num1,int num2){if (num1==num2) {return (NO);}else {return (YES);}//将BOOL值转为相应的NSString类型
NSString *boolString(BOOL noYes){if (noYes==NO) {return (@"NO");}else {return (@"YES");}}
//在main函数中调用上面方法
int main (int argc,constchar * argv[]) { BOOL areTheyDifferenr;areTheyDifferenr=areIntsDifferent(5,5);NSLog(@"are %d and %d different? %@",5,5,boolString(areTheyDifferenr));areTheyDifferenr=areIntsDifferent(23,42);NSLog(@"are %d and %d different? %@",23,42,boolString(areTheyDifferenr)); return0;}运行结果:总结:Objective-C中的BOOL类型与C和JAVA很像,用法也比较简单,需要注意的是Objective-C的BOOL类型是YES值和NO值,而不是true和false。