The property modifier
Readonly This tag indicates that the property is read-only. The default tag is read/write. If you specify read-only, you only need one reader in the @implementation. Or if you use the **@synthesize keyword, there’s also a get method ** that gets parsed. And if you try to assign a value to a property using the dot operator, you will get a compilation error.
Readwrite This flag indicates that the property is treated as read and write, which is also the default modifier. Both the setter and the reader need to be implemented in the @implementation. If the **@synthesize keyword is used, both get and set methods are parsed.
The assign tag indicates that the setter assigns values directly, which is also the default. In an application that uses garbage collection, if you want an attribute to use assign and the class complies with the NSCopying protocol, you should specify this tag instead of simply using the default, otherwise you’ll get a compile warning. Again, this tells the compiler that you really need to assign, even if it’s copiable.
Strong Strong indicates that the owning property owns a strong reference to a value. As a whole, it is possible to make a owning relationship in which the new value is kept and the reference count of the new value is increased, and then the old value is released and the reference count of the old value is reduced. Only objects can be decorated. Use strong if you want to keep a strong reference to some objects.
Weak indicates that owning a weak reference to the assigned value object is a nonowning relationship in which the new value does not increase the reference count and the old value does not decrease the reference count. The weak attribute is automatically set to nil after the assigned value is destroyed at a reference count of 0 to prevent wild pointer errors. Weak is often used to modify scenarios such as the delegate to prevent circular references.
Copy Indicates that a copy of the passed value is used when assigning a value. Copy is performed by the copy method, and this property is only valid for object types that implement the NSCopying protocol. Note: If the property is a mutable array, it will become an immutable array and crash easily
Atomic /nonatomic Default: Atomic specifies whether the synthesized access method is atomic or not, which can be interpreted as thread-safe. Using atomic on iOS is not necessarily thread-safe either. As you can see, nonatomic properties are used in almost all code Settings, which can improve access performance. Using locking in iOS is expensive and degrades performance.
Conclusion: The default keywords for base data are atomic, readwrite, assign, and common OC objects are atomic, readwrite, and strong.
At sign property, at sign synthesize, and at sign dynamic
@ Property nature:
@property = ivar(instance variable) + getter/setter (access method);Copy the code
Once the attributes are defined, the compiler automatically writes the methods needed to access them, a process called autosynthesis. It is important to note that this process is implemented by the compiler at compile time, so the source code for these synthesized methods is not available in the editor. In addition to generating getters and setters for the method code, the compiler automatically adds instance variables of the appropriate type to the class and precedes the attribute names with an underscore as the name of the instance variable.
Every time we add a property, the system adds a description of a member variable to ivar_list, a description of setter and getter methods to method_list, a description of a property to the property list, and then calculates the offset of that property in the object, and gives the The corresponding implementation of setter and getter methods assigns values from the offset in setter methods and values from the offset in getter methods. The pointer type of the offset of the system object is type-cast in order to be able to read the correct number of bytes.
At sign property has two words, at sign synthesize, and at sign dynamic. If at sign syntheszie and at sign dynamic don’t write, then the default is @syntheszie var = _var;
@synthesize @synthesize means that if a property doesn’t manually implement setter and getter methods, the compiler automatically adds them.
@dynamic
@dynamic tells the compiler that setter and getter methods for a property are implemented by the user and not automatically generated. If a property is declared as @dynamic var, and you don’t provide @setter and @getter methods, this is fine at compile time, but when the program runs to instance.var = someVar, the program will crash because of the lack of setter methods. Or when someVar = var is run, it will also crash due to the lack of getter methods. Compile time is fine, and the corresponding method is executed at run time, which is called dynamic binding.
-
1.BAT and other major manufacturers iOS interview questions + answers
-
2.Must-read books for Advanced Development in iOS (Classics must Read)
-
3.IOS Development Advanced Interview “resume creation” guide
-
(4)IOS Interview Process to the basics