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:
- [shape draw]
I. Class declaration Interface ):
- @interface TestCoop : NSObject {
- int iMonth;
- int iYear;
- int iDay;
- }
-
- - (void) setYear: (int) iYear;
- - (void) primalSetData: (int)iYear :(int)iMonth :(int)iDay;
- - (void) setData: (int)Year iMonth:(int)iMonth iDay:(int)iDay;
- - (void) displayDateInfo;
-
- @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:
- -(Void) setDay: (int) iDay; // a single parameter
- -(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:
- -(Void) setData: (int) Year iMonth :( int) iMonth iDay :( int) iDay; // multiple parameters
Apple recommends the second method, though tedious.
Ii. Class implementation:
- @implementation TestCoop
- - (void) displayDateInfo{
- NSLog(@"Today is: %d.%d.%d\n", iYear, iMonth, iDay);
- }
-
- - (void) setYear: (int) year{
- iYear = year;
- }
-
- - (void) primalSetData: (int)year :(int)month :(int)day{
- iYear = year;
- iMonth = month;
- iDay = day;
- }
-
- - (void) setData: (int)year iMonth:(int)month iDay:(int)day{
- iYear = year;
- iMonth = month;
- 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:
- - (void) setYear: (int) year{ //right
- iYear = year;
- }
The change is the same as that in the clear time, as shown below:
- - (void) setYear: (int) iYear{ //error
- 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
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
-
- // insert code here...
- //NSLog(@"%d-,%s %@\n", 12, "hel123lo", @"123");
-
- TestCoop *ptest = [TestCoop new];
- [ptest primalSetData :2009 :03 :05];
- [ptest displayDateInfo];
- [ptest setData:2010 iMonth:06 iDay:06];
- [ptest displayDateInfo];
- [ptest setYear:1987];
- [ptest displayDateInfo];
- [pool drain];
- return 0;
- }
The following information is output after running:
- Today is: 2009.3.5
- Today is: 2010.6.6
- 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:
- TestCoop *ptest = [[TestCoop alloc] init];
Summary: UnderstandingObjective-CI hope this article will be helpful to you!