1. for the use of self in the instance method, objc is special. for example, self. name actually accesses the property name instead of the internal variable, that is, It accesses the setter/getter method.
@ Interface person: nsobject
{
Nsstring * Name;
}
@ Property (copy) nsstring * Name;
@ End
@ Implementation person
- ( Void ) Dosomething {
Name = @" Fred " ; // Access Ivar directly!
Self. Name = @" Fred " ; // Callaccessor method.
}
@ Sythesize name;
@ End
. H file, Declaration class, with private variables in braces
# Import < Foundation / Foundation. h >
@ Interface person: nsobject
{
// Instance variables
Nsstring * Name;
Int Age;
}
// Method declarations
- (Nsstring * ) Name;
- ( Void ) Setname :( nsstring * ) Value;
-(Int) Age;
-(Void) Setage :(Int) Age;
-(Bool) canlegallyvote;
-(Void) Castballot;
@ End
Implementation class
# Import " Person. h "
@ Implementation person
-(Int) Age {
ReturnAge;
}
-(Void) Setage :(Int) Value {
Age=Value;
}
//... And other methods like super. INIT ();
@ End
The object can be created in two steps: + alloc and-init. The former is memory allocation (requiring release) and the latter is initialization.
-Retain is to increase the count
-Release is used to reduce the count.
-The dealloc system automatically calls this function when the retain count is 0.
[Nil dosomething]; no response method
@ Property (assign) nsstring * Name; // pointer assignment
@ Property (retain) nsstring * Name; // retain called
@ Property (copy) nsstring * Name; // copy called