1. @ property int age;
In the case of a compiler, it will automatically compile and expand:
<Age uppercase letters in setter,
Point syntax is P. Age
>
-(Void) setage :( INT) age;
-(INT) age;
2. Likewise: @ property int _ age;
In the case of a compiler, it will automatically compile and expand:
<_ Age in the setter, the first letter is uppercase, which is a horizontal line, and the upper letter is kept. The point syntax is P. _ age>
-(Void) set_age :( INT) age;
-(INT) _ age;
Generally, use 1 instead of 2.
@ Synthesize age = _ age;
Access the member variable "_ age". If the member variable "_ age" does not exist, the system will automatically generate the member variable "@ private" for "_ age" and automatically generate the setter and getter of "age ".
After version 4.5, You can omit @
Synthesize,
@ Property int age; three functions
1. Generate
Setter and getter Declaration
2. Generate a member variable for _ age
3. Generate
Implementation of setter and getter
// Note: you must go to "project --" building setting -- "All --" objcet-C automic reference counting under non-arc.
// @ Property (nonatomic, retain) nsstring * name is equivalent:
-(Void) setname :( nsstring *) Name
{
If (_ name! = Name)
{
[_ Name release];
_ Name = [name retain
];
}
}
Otherwise, the operation fails.
[_ Name release];
_ Name = [name retain
];
// @ Property: the setter and getter declarations of a member variable can be automatically generated.
@ Property int age;
//-(Void) setage :( INT)Age;
//-(INT) age;
@ Property int height;
//-(Void) setheight :( INT)Height;
//-(INT) height;
Can be abbreviated as @ property int age, height;
Note: The age, height, and not the _ age and _ height member variables are generated.
Semantic settings (assign, retain, copy)
Assign is used for basic data types; copy is used for nsstring (retain can also be used); retain is used for non-nsstring objects
Example:
@ Property (nonatomic, assign) int age;
@ Property (nonatomic, copy) nsstring * Name;
@ Property (nonatomic,
Retain
) Nsstring * number;
@ Property (nonatomic, retain) nsarray * group;
@ Synthesize age = _ age;
// @ Synthesize automatically generates the setter and getter implementations of age, and accesses the member Variable _ age, which is automatically generated after 4.4.
# Import <Foundation/Foundation. h>
@ Interface car: nsobject
{
// Int _ speed;
// Int _ wheels;
}
@ Property int speed;
@ Property int wheels;
// @ Property int speed, wheels;
-(Void) test;
@ End
# Import "car. H"
@ Implementation car
//@ Synthesize speed = _ speed, wheels = _ wheels;
// Note: the member variable "_ speed" is accessed. If the member variable "_ speed" does not exist, the variable "@ private" is automatically generated.
@ Synthesize speed = _ speed;
@ Synthesize wheels = _ wheels;
-(Void) test
{
Nslog (@ "speed = % d", _ speed );
}
@ End
Likewise:
@ Synthesize
Age;
// The age member variable is accessed by default. If no age exists, the @ private type age variable is automatically generated.
@ Property int age;
If. M contains setter or getter, the system generates member variables and getter or setter. If
If setter has another getter, it will no longer generate
Getter, setter, and member variables, because the member variables serve the setter and getter services. At this time (none ),
@ Synthesize
Age = _ age; cannot be omitted.
ID is equivalent to nsobject *. A universal pointer that can point to or operate any OC object.
Property, synthesize, ID