Learn about some basic operations in Objective-C. This document is required for beginners.

Source: Internet
Author: User

UnderstandingObjective-CSome basic operations for beginners are described in this article.Objective-CClass declaration, definition, instance, initialization content, let's look at the details.

Objective-CThe calling method is called sending a message, and the notification object performs an operation. Syntax:

 
 
  1. [shape draw] 

I. Class declaration Interface ):

 
 
  1. @interface TestCoop : NSObject {  
  2.     int iMonth;  
  3.     int iYear;  
  4.     int iDay;  
  5. }  
  6.  
  7. - (void) setYear: (int) iYear;  
  8. - (void) primalSetData: (int)iYear :(int)iMonth :(int)iDay;  
  9. - (void) setData: (int)Year iMonth:(int)iMonth iDay:(int)iDay;  
  10. - (void) displayDateInfo;  
  11.  
  12. @end 

1. The short line/-above indicates that this isObjective-CMethod declaration, used to distinguish the method of method declaration in C language of the function prototype) from Objective-C. The short line is followed by the return type of the method, for example (void), in parentheses.

1.1 note that the declaration of the method is behind the brackets, and only the variable definition is in the {} area before @ end, which is very different from that in C ++.

2. The return type is followed by the function name, which is the same as the C language. The difference is that the parameter declaration method

2.1 If a function without parameters is followed by no parentheses or colons, the function name ends with a plus sign, for example,-(void) displayDateInfo;

2.2 Add a colon and a parameter type name after a parameter, for example:

 
 
  1. -(Void) setDay: (int) iDay; // a single parameter
  2. -(Void) primalSetData: (int) iYear :( int) iMonth :( int) iDay; // multiple parameters

Objective also provides an infix syntax. The method names and parameters are the same:

Add an identifier before the parameter, which is usually the same as the variable name, as shown below:

 
 
  1. -(Void) setData: (int) Year iMonth :( int) iMonth iDay :( int) iDay; // multiple parameters

Apple recommends the second method, though tedious.

Ii. Class implementation:

 
 
  1. @implementation TestCoop  
  2. - (void) displayDateInfo{  
  3.     NSLog(@"Today is: %d.%d.%d\n", iYear, iMonth, iDay);  
  4. }  
  5.  
  6. - (void) setYear: (int) year{  
  7.     iYear = year;  
  8. }  
  9.  
  10. - (void) primalSetData: (int)year :(int)month :(int)day{  
  11.     iYear = year;  
  12.     iMonth = month;  
  13.     iDay = day;      
  14. }  
  15.  
  16. - (void) setData: (int)year iMonth:(int)month iDay:(int)day{  
  17.     iYear = year;  
  18.     iMonth = month;  
  19.     iDay = day;  

1. Note: Some function parameters of the method implementation of the class cannot be the same as those of the Declaration, that is, they cannot be the same as the class variables, otherwise the initial variables will be hidden.

For example:

 
 
  1. - (void) setYear: (int) year{ //right  
  2.     iYear = year;  

The change is the same as that in the clear time, as shown below:

 
 
  1. - (void) setYear: (int) iYear{ //error  
  2.     iYeariYear = iYear;  

Obviously, the xcode compilation reports warnging, that is, the initial variables are hidden. In fact, the essence is the scope of the variables. Your local variables are the same as the class variables, but of course they cannot be accessed.

Root Cause: When declaringObjective-CI like to replace the function parameter name with the name of the class variable. I don't know why. It's really a tangled problem. If you do not need it from the Declaration, you will be OK, and you will not need to change it at the definition. You do not know why Apple did that ).

Iii. instantiate an object

 
 
  1. int main (int argc, const char * argv[]) {  
  2.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  3.  
  4.     // insert code here...  
  5.     //NSLog(@"%d-,%s %@\n", 12, "hel123lo", @"123");  
  6.       
  7.     TestCoop *ptest = [TestCoop new];   
  8.     [ptest primalSetData :2009 :03 :05];  
  9.     [ptest displayDateInfo];  
  10.     [ptest setData:2010 iMonth:06 iDay:06];  
  11.     [ptest displayDateInfo];  
  12.     [ptest setYear:1987];  
  13.     [ptest displayDateInfo];  
  14.     [pool drain];  
  15.     return 0;  

The following information is output after running:

 
 
  1. Today is: 2009.3.5  
  2. Today is: 2010.6.6  
  3. Today is: 1987.6.6 

You can create an object by sending a new message to the class of the object to be created.

Then, send various messages to the object to operate on the object.

// Class initialization

However, cocoa is used to using alloc and init to create objects, rather than new

Use alloc to allocate memory and use init for initialization. The memory will be cleared from 0, the bool type is NO, the int type is 0, and the pointer is nil.

The above object creation code is changed to the following:

 
 
  1. TestCoop *ptest = [[TestCoop alloc] init]; 

Summary: UnderstandingObjective-CI hope this article will be helpful to you!

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.