Objective-c Study Notes-Basic Content

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.