------iOS training, Java training, Android training, iOS learning technology blog, looking forward to communicating with you------
Dot syntax
The essence of point syntax is method invocation: Invoking the setter and getter of an object member variable
is the compiler feature, the compiler helps to turn
P.age = 25; Equivalent to [P setage:25];
int a = P.age; equivalent to int a = [P age];
Access member variables cannot be in dot syntax, but
p->_age;
Scope of the attribute (instance variable \ member variable)
@public can access the object's member variables directly from anywhere using the object p->_age=25;
@protected (Default-declared in @interface) can be accessed directly in the object methods of the current class and subclass
@private (Default-declared in @implementation) can only be accessed directly in the object method of the current class
A class method cannot access a member variable because the class method is called through the class name, is not dependent on the object, and the member variable is based on the object being created.
The member variables declared in the. h file @interface By default are @protected------member variables written in the. m file @implementation are @private
member variables with the same name cannot be declared in the @interface and @implementation
@property and @synthesize
. h file
@property can automatically generate a declaration of the setter and getter for a member variable
@property int age;
@property NSString *name;
. m file
The @synthesize automatically generates the @property of the age setter and getter in the _age, and accesses the member variable
@synthesize Age=_age;
@synthesize Name=_name;
The above two lines are equivalent to
@synthesize Age=_age, Name=_name; The 2 member variables of the _age and _name are accessed, and if the 2 member variables do not exist (not declared in the. h file @interface), the member variables of the @private type are automatically generated _age and _name
The current version of Xcode does not need to be written in the. m file again @synthesize;
You only need to @property int Age in the. h file;
The compiler automatically generates @private member variable int _age simultaneously generates a setter, Getter Declaration and implementation
Id
ID is a data type---Universal pointer that can point to \ manipulate any OC object
ID equals NSObject *
Definition of ID Type:
typedef struct objc_object{
Class Isa;
} *id;
Construction Method-Used to methods for initializing objects (-init,-initwithxxx)
First, overriding the construction method
Complete creation of an available object [the person New],new method calls the following 2 methods
1> Allocate storage space + Alloc returns an unusable object with allocated storage space (not initialized yet)
2> Initialization-init (this initialization is too rigid, the development of the general self-written construction method initialization-initwithxxx, so generally do not use + new)
Overriding the purpose of the construction method: In order for the object to be created, the member variable has some fixed value
Overriding Construction methods Note: overriding in. m files of a class-init method
1> first calls the constructor method of the parent class [Super Init];
2> initialization of a subclass's internal member variables
Override constructor Method init
-(ID) init
{
///1> Be sure to call the-init method of the parent class first : (Initialize some member variables and other attributes declared in the parent class)
self = [super init]; // returns the current object Self
//2> If the initialization of the object succeeds, it is necessary to initialize the member variable.
if (nil! = self ) //0! = Self Nil is 0
{
// initialization succeeded
_age = 3; // Initialize the member variable here (initializes the member variable of the current object)
}
//3> Returns an initialized object
return self ;
}
Simplified construction method after ——— rewrite init
-(ID) init
{
if (self = [super init])
{
_age = 5; // Initialize the member variable here (initializes the member variable of the current object)
}
return self ;
}
Ii. Custom Construction methods
/*
specification for custom construction methods
1> must be an object method, with - start
2> return value is typically an ID type
3> method names usually start with Initwith
*/
-(ID) initwithname: (nsstring *) name
{
if ( self = [super init])
{
_name = name;
}
return self ;
}
Category ()
Category categories, categories and categories:
You can extend some methods to a class ---Do not modify the original class
1> declaration. h file
@interface to increase class name (category name) of the taxonomy
@end
2> implementing. m files
@implementation to increase class name (category name) of the taxonomy
@end
Note:
1> classification can only increase the method, cannot increase the member variable
2> methods added in the taxonomy can access member variables declared in the original class
3> classification can re-implement (overwrite) the same name method in the original class, causing the original method to fail (it is not recommended to write a method with the same name as the original class in the taxonomy, because the original method is overwritten)
4> Method Invocation Priority: Classification (last compiled classification first) > Original class > Parent class
Dark Horse Programmer---OC---point syntax, attribute scope, @property and @synthesize, ID, construction method, classification