BOOL
首先編寫程序:
#import <Foundation/Foundation.h>
BOOL areIntDifferent(int ver1 , int ver2){
if(ver1 == ver2){
return (NO);
}else{
return (YES);
}
}
NSString* boolString(BOOL yesNo){
if(yesNo == NO){
return (@"NO");
}else{
return (@"YES");
}
}
int main(int argc , const char *argv[]){
BOOL isDiff;
isDiff = areIntDifferent(6,6);
NSLog(@"are %d and %d different ? %@" , 6,6,boolString(isDiff));
isDiff = areIntDifferent(29,48);
NSLog(@"are %d and %d different ? %@" , 29,48,boolString(isDiff));
return (0);
}
然後在相同目錄編寫GNUMakefile文件include $(GNUSTEP_ROOT)/common.make
TOOL_NAME = CompareTwoNum
CompareTwoNum_OBJC_FILES = compare.m
include $(GNUSTEP_ROOT)/tool.make
執行命令:
make 編譯成功 , 執行文件 ./obj/CompareTwoNum , 輸出如下結果:
2014-05-06 05:26:58.665 CompareTwoNum[3277] are 6 and 6 different ? NO
2014-05-06 05:26:58.666 CompareTwoNum[3277] are 29 and 48 different ? YES
注意事項:1、在areIntDifferent函數中不要使用 以下語句替代函數中的內容
return ver1 - ver2 ;
2、 不要用BOOL 值和 YES 比較 , 和NO比較一定安全。(OBJC中1不等於YES)
變量 + 循環
#import <Foundation/Foundation.h>
int main(int argc , const char *argv[]){
int count = 3 ;
NSLog(@"the number from 0 to %d is : ",count);
int i ;
for(i = 0 ; i <= count ; i++){
NSLog(@"%d ", i);
}
return (0);
}
GNUMakefile 文件內容參見上文。字符串操作 + strlen (求字符串長度函數)
#import <Foundation/Foundation.h>
int main(int argc , const char * argv[]){
const char * words[4] = {"hello" , "zhangting" , "latitude" , "longitude"};
int count = 4 ;
int i ;
for(i = 0 ; i < count ; i++){
NSLog(@"the length of %s is %d" , words[i] , strlen(words[i]));
}
return 0;
}
文件操作 + strlen
#import <Foundation/Foundation.h>
int main(int argc , const char * argv[]){
FILE * rf = fopen("tmp.txt" , "r");
char word[256];
while(fgets(word,256,rf)){
word[strlen(word) -1] = '\0';
NSLog(@"The length of %s is %d ." , word , strlen(word));
}
fclose(rf);
return (0);
}
執行該文件之前需準備好文件 tmp.txt
fgets 會連每行的換行符都讀取,這樣會佔用一個字符的長度,所以使用
word[strlen(word) -1] = '\0';
將該換行符替換爲字符串結尾符號。objective-c 中的消息發送表示
[object say] : 表示向object對象發送say 消息
objective-c 中新術語
方法: 爲響應消息而運行的代碼。
方法調度程序 :Objective-c 使用的一種用於推測執行什麼方法來響應特定的消息。
接口:對象的類應該提供的操作。
實現:使接口正常工作的代碼。