(IOS) Understanding of @ property and @ synthesize (original), iossynthesize
When I started to learn ios, I didn't understand some objc syntaxes, such as @ property and @ synthesize. I used to remember and use them, but I wrote more code, I have some understanding of objc and ios, and recently used MRC, So I discussed it in the Process of memory detection and variable usage, in the end, I had a certain understanding of their functions.
Generally, @ property and @ synthesize are used together. The variables declared by @ property are processed by setter and getter by default.
Property can declare various attributes of an attribute.
1.Access Method for declaring attributes:
- Getter = getterName
- Setter = setterName
Declares the access attribute settings and method names.
2.Permission for declaring attribute write operations:
- Readwrite
Declare this attribute as a read/write attribute, that is, you can access the setting method (setter), or you can access the getter method (getter), which is mutually exclusive with readonly.
- Readonly
Declare this attribute as a read-only attribute and only access the getter corresponding to this attribute. It is mutually exclusive with readwrite.
3.Implementation of declarative write Methods
4.Atomicity of access methods
For more information about atomicity, check the atomicity of the operating system.
@ Synthesize defines the names of getter and setter, which are different from the variable names, so as to protect the variables from improper access.
Function:Let the compiler automatically write a method declaration with the same name as the data member to save the declaration of the read/write method.
[Objective-C beginner's question] For @ property and @ synthesize
If you are an iOS 6.0 SDK, There is nothing strange. To declare an attribute in iOS 6.0, you only need @ property.
After @ property and @ synthesize, what does the generated setter do?
@ Property (nonatomic, copy) the code generated by setter is roughly as follows:-(void) setArrayList: (NSMutableArray *) newArrayList {NSArrayList * copy = [newArrayList copy]; [arrayList release]; arrayList = copy;} because the @ property (nonatomic, copy) attribute is declared, the copy method is called by default, while the NSMutableArray copy method returns the NSArray type, therefore, addObject cannot be called. You can only use mutableCopy. Unfortunately, @ property does not support the declaration of mutableCopy.