參照書中關於布爾類型實例,敲出下面代碼
#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。