This section discusses how to use declarative attributes and the automatically generated method inside the system as the property access interface, and the instance variable Management introduced in this section.
The declarative properties declared properties can be used to conveniently implement the accessors method accessor methods.
Declarative attributes provide a clear and clear specification for the visitor's method behavior.
The compiler helps you extend the application attributes to the accessors to reduce the numberCodeQuantity.
Declarative attributes are also a type of identifier and have their own scopes, so the compiler can detect undeclarative attributes.
Syntax:
@ Property (attributes) type name;
For example:
@ Interface myclass: nsobject
{
Float value;
}
@ Property float value;
@ End
Setter semantics:
Assign: simple assignment
Retain: the previous value receives a release message.
Copy: Copy
If you do not use the Garbage Collector, you must specify assign/retain/copy. Otherwise, you will receive a warning.
Atomicity and non-atomicity can be specified.
If it is retain/copy, an internal lock is automatically used to implement atomicity when automic is not specified.
If you use the garbage collection mechanism, you can specify whether the reference is a strong reference or a weak reference, but this is informal.
@ Synthesize firstname, lastname, age = yearsold;
Attribute declaration with instance variable name specified.
Whether or not you specify the instance variable name, this variable is only valid for the current class and not visible to the parent class.
@ Dynamic
About using @ Properties
Any object-C class can be declared.
You can declare it again in the subclass, and Modifier
Copy:
This property is useful in some situations. For example, you need to assign a mutable string to ensure that subsequent modifications do not affect the variable that has been assigned a value.
Non-asign property should
-(Void) dealloc {
[Property release];
[Super dealloc];
}
Or (new version, because instance variables cannot be directly accessed)
-(Void) dealloc {
[Self setproperty: Nil];
[Super dealloc];
}
The property in the subclass can use writable to cover the readonly attribute in the parent class.
The impact of different attributes on performance.
In the old environment, the @ synthesize attribute needs to manually declare the instance variable, but not in the new system. The system will automatically help you.
This section involves different types of attributes and the resulting differences in the value assignment mode, runtime performance, Operation atomicity, thread security, backward compatibility, and other issues. You need to take a closer look at these issues.