In objective-C, the call method is calledSend message
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. PreviousShort-term /-
This is the declaration of the objective-C method, which is used to distinguish the method of declaring the function prototype (in C Language) from the method (in objective-C. The method behind the short lineReturn type/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 does not need to be followed by parentheses or colons, it is directly the function name end plus points, such:-(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;
}
If it is the same as when it is declared, it is as follows:-(void) setyear: (INT) iyear {// Error
Iyear = 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 when defining it. I don't know why Apple did that ).
Iii. instantiate an object
Int main (INT argc, const char * argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool 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];