BOOL
First, write the program:
#import
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);}
Then write the GNUMakefile file in the same directory.
include $(GNUSTEP_ROOT)/common.makeTOOL_NAME = CompareTwoNumCompareTwoNum_OBJC_FILES = compare.minclude $(GNUSTEP_ROOT)/tool.make
Run the following command:
Make is compiled successfully. Execute the file./obj/CompareTwoNum and output the following results:
2014-05-06 05:26:58.665 CompareTwoNum[3277] are 6 and 6 different ? NO2014-05-06 05:26:58.666 CompareTwoNum[3277] are 29 and 48 different ? YES
Note:
1. Do not use the following statement in the areIntDifferent function to replace the content in the function.
Return ver1-ver2;
2. Do not compare BOOL values with YES values. It is safer than NO.(1 in OBJC is not equal to YES)
Variable + Loop
#import
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);}
For details about the GNUMakefile file, see the preceding section.
String operation + strlen (evaluate the string length function)
#import
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;}
File Operations + strlen
#import
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);}
Prepare the tmp.txt file before executing the file.
Fgets reads the line breaks of each line, which occupies the length of one character.
word[strlen(word) -1] = '\0';
Replace the linefeed with the end character of the string.
Message sending representation in objective-c
[Object say]: sends a say message to the object.
New terminology in objective-c
Method: code to respond to a message.
Method Scheduler: a method used by Objective-c to speculate what method is executed to respond to a specific message.
Interface: The operation that the object class should provide.
Implementation: code that allows the interface to work normally.