歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Objective-C學習——布爾類型

Objective-C學習——布爾類型

日期:2017/3/1 10:03:01   编辑:Linux編程

參照書中關於布爾類型實例,敲出下面代碼


#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。

Copyright © Linux教程網 All Rights Reserved