1: Understanding the concept of attributes
Property automatically generates access methods, which can be called using dot syntax,
If you do not want the compiler to automatically synthesize the access method, you can do it yourself, there is another way, is to use the @dynamic keyword, it will tell the compiler, do not automatically create the implementation of the properties of the instance variables and access methods, and when compiling the Access property code, even if the compiler found that the access method is not defined, will not error, it is believed that these methods will be found in the runtime, using the method is to define the properties in the header file (. h file), the implementation file (. m file) using the @dynamic attribute name;
Once you have defined a property, you can specify the name of the instance variable by @synthesize syntax in the class's implementation code (that is,. m file) instead of using the default name, but this method is generally not recommended because it is not easy to read
Attribute traits
Attributes can have traits that fall into four categories:
Atomic Nature
Definition: in concurrent programming. If the operation is holistic, that is, the rest of the system cannot observe the temporary results generated by its intermediate steps, but only the results before and after the operation, then the operation is ' Atomic ' (atomic), or the operation is ' Atomic ' (ATOMICTY)
By default, a method synthesized by the compiler ensures its atomicity through the locking mechanism (atomicity), and if the attribute has a nonatomic trait, no sync lock is used, note that although there is no trait named ' atomicity ', if the property does not have a Nonatomi c Trait, it is ' atomic ', it can also be stated in the attributes of the attribute, the compiler will not error
The difference between atomic and nonatomic
The acquisition method with the atomic trait ensures the atomicity of its operation through a locking mechanism. That is, if two threads read a property at the same time, then whenever a valid property value is always obtained (when thread a writes, the other thread's read or write operation waits for that operation.) When the write operation of the A thread finishes, the B thread writes, and all the operations on the different threads are executed sequentially-that is, if a thread is executing getter/setter, the other threads will have to wait. )
With Nonatomic, if one of the threads is changing its property value, another thread may break in and read the property value that has not been modified, in which case the thread reads the property value definitely inconsistent
In a generic iOS program, all attributes are declared as nonatomic, because the overhead of using a sync lock in iOS is large, with performance issues, and generally does not require that the attribute must be atomic, and even if the atomic is set, it does not guarantee absolute thread safety
Read/write permissions
Attributes with ReadWrite (readable and writable) traits have a Get method (getter) and a set method (setter),
Properties with ReadOnly (read-only) attributes only have a Get method
Memory Management semantics
The following set of traits only affects the ' setup method ', when the compiler is synthesizing access code, to prove this trait to determine the generated code, and if you write an access method, you must match the attributes of the property.
The Assign setting method only performs simple assignment operations of a scalar type (such as CGFloat or Nsinteger)
Strong this trait indicates that the attribute defines an ' owning relationship ', when a new value is set for this property, the setting method preserves the new value, releases the old value, and then sets the new value up
Weak this trait indicates a ' non-owning relationship ', when setting a new value for this property, the setting method neither preserves the new value nor releases the old value, but the property value is also emptied when the object referred to by the property is destroyed
The relationship described in copy of this trait is similar to strong, however, the set method does not retain the new value but copies it, which is often used to protect its encapsulation when the property type is NSString *, because the new value passed to the set method may point to a Nsmublestrin An instance of Class G, which is a subclass of NSString that represents a string that can modify its value, and if the string is not copied, the value of the string may be altered without the object's knowledge, as long as the object used to implement the property is ' mutable '. A copy should be copied when setting a new property value.
Method name
The method name of the access method can be specified by the following attributes:
getter=<name> Specifies the method name of ' Get method ' if a property is Boolean and you want to get the method plus ' is ' prefix. Then you can use this method to specify
For example: @property (nonatomic, Getter-ison) BOOL on;
Setter=<name> specifying method name for ' Set method '
With these qualities, you can fine-tune the access method synthesized by the compiler, but it is important to note that if you implement these access methods yourself, you should ensure that they have the attributes declared by the relevant properties.
If you want to set property values in other methods, follow the same semantics declared in the attribute definition, for example: A class has a new initialization method
@interface Eocperson:nsmanagedobject
@property (copy) NSString * FIRSTNAME;
@property (copy) NSString * LASTNAME;
-(ID) Initwithfirstname: (nsstring*) firstName lastName: (NSString *) lastName;
@end
When implementing this custom initialization method, be sure to follow the ' copy ' semantics in the attribute definition, because ' attribute definition ' is equivalent to the contract reached between ' class ' and ' property value to be set '
The initialization method can be written like this:
-(ID) Initwithfirstname: (nsstring*) firstName lastName: (NSString *) lastname{
if (self = = [Super init]) {
_FirstName = [firstName copy];
_lastname = [lastName copy];
}
}
Summarize:
You can specify the correct semantics for storing your data through ' traits '
When setting an instance variable corresponding to a property, be sure to follow the semantics declared from that property
IOS-Effective OBJECTIVE-C Read notes (3)