Attribute Definition
@ Interface nothin: nsobject
@ Property nsstring * prop;
@ End
Access attributes
Nothin * nothin = [nothin new];
// Get
[Nothin prop];
// Set
[Nothin setprop: @ "nothin"];
After @ property is declared, the compiler automatically integrates the getter and setter attributes.
Attribute access method naming rules:
The getter method and attribute name are the same. For example, the getter method of prop is also named prop.
The setter method is an attribute name prefixed with "set" and capitalized with the first letter. For example, the setter method of prop is called setprop.
The default attribute is readwrite. Add readonly attribute: @ property (readonly) nsstring * prop to the attribute.
Set the custom access method: @ property (getter = getprop) nsstring * prop;
If multiple attributes are set at the same time, separate them with commas: @ property (ready, Getter = getprop) nsstring * prop;
Simple syntax for access attributes:
// Get
Nsstring * STR = nothin. firstname;
// Set
Nothin. firstname = @ "nothin ";
By default, the compiler automatically encapsulates an instance variable for the property. The instance variable is prefixed with an underscore in the property name. For example, the instance variable encapsulated by the prop property is _ prop.
You can directly access the instance variable within the class.
-(Void) Method
{
_ Prop = @ "nothin ";
}
You can also enable attributes to encapsulate custom instance variables by using the synthesize keyword in the. M file. For example:
@ Synthesize prop = custom_prop;
If @ synthesize is used but no instance variable name is specified for the attribute, the instance variable name encapsulated by this attribute is the same as the attribute name. For example
@ Synthesize prop; in this case, the instance variable name is also prop
If you do not want to use attributes to encapsulate instance variables, you can also define instance variables in. h or. M:
@ Interface nothin: nsobject
{
Nsstring * _ var;
}