Learn about Objective-C Class declarations

Source: Internet
Author: User

AboutObjective-C class declarationRelevant learning documents are the content to be introduced in this article.Objective-C class declaration, Definition, instance, initialization.Objective-CThe calling method is called sending a message, and the notification object performs an operation. Syntax: [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-term/-above indicates that this is the declaration of the Objective-C method, which is used to distinguish the method declared in the C language of the function prototype) from the method declared in 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. - (void) setYear: (int) year{  
  6.     iYear = year;  
  7. }  
  8. - (void) primalSetData: (int)year :(int)month :(int)day{  
  9.     iYear = year;  
  10.     iMonth = month;  
  11.     iDay = day;      
  12. }  
  13. - (void) setData: (int)year iMonth:(int)month iDay:(int)day{  
  14.     iYear = year;  
  15.     iMonth = month;  
  16.     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,

Of course, the access fails.

Root Cause: When declared, Objective-C prefers to replace the function parameter name with the class variable name. I don't know why. It's really a tangle. 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: AboutObjective-C class declarationI hope this article will help you with the introduction of relevant learning documents!

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.