For details, please see the text description below.This article is very detailed.

1. Readability:readonly, readwrite

@property (readwrite, ....) valuetype value;

This property is the default property of the variable,If you (neither readwrite nor readonly are used,Then your variable is the readwrite attribute), and by adding the readwrite attribute your variable will have get and set methods.

property (readonly, ...) valuetype value;

This attribute variable indicates that the variable has only readable methods.That is,You can only use its get method.

2. Assign, setter methods directly assign values,Does not perform any retain operations, in order to solve the problem of primitive types and circular references

3.Retain, setter method releases the old value of the parameter and then retains the new value, all implementations are in this order

4. Copy, setter method for copy operation, the same as the retain process,First the old value release, then copy out the new object,retaincount is 1. This is a mechanism introduced to reduce the dependency on context.

5 .nonatomic, nonatomic access,Without synchronization,Multi-threaded concurrent access improves performance.

Note that if this attribute is not added,The default is that both access methods are atomic transaction access.The lock is added to the owning object instance level. So it is safe to use multi-threading without nonatomic.

6. Retain vs. copy

copy:create an object with an index count of 1 and then release the old object

retain:release the old object,Assign the value of the old object to the input object,Increase the index count of the input object to 1

What the hell does that mean?

copy actually creates an identical object,While retain is not:

For example, define the following attributes:

@property (copy, nonatomic) nsstring * teststr;

Use as follows:

nsmutablestring * str3=[[nsmutablestring alloc] initwithstring:@"mutable string"
nslog (@ "%d", [self.teststr retaincount]);
nslog (@ "%d", [str3 retaincount]);

You can see that teststr and str3 have different addresses.retaincount is 1

If copy is changed to retain, then they point to the same address,retaincount is 2.

you got it,retain is a pointer copy, pointing to the same address,The count is incremented by one, and copy copies the contents.

Objective-c attribute properties (assign, retain, copy, readonly, readwrite, atomic, nonatomic)

assign:Specify the setter method to use simple assignment,This is the default action.You can use this attribute for scalar types (such as int).You can imagine a float, it is not an object,So it cannot retain or copy.

retain:specifies that retain should be called on subsequent objects,The previous value sends a release message. You can imagine an nsstring instance, it is an object,And you might want to retain it.

copy:Specifies that a copy of the object (deep copy) should be used. The previous value sends a release message. Basically like retain, but without increasing the reference count,It is allocating a new piece of memory to place it.

readonly:Only getter methods will be generated without setter methods (getter methods have no get prefix).

readwrite:default attribute,Getters and setters will be generated without additional parameters (the setter method has only one parameter).

atomic:for the object's default properties,The setter/getter generated method is an atomic operation.If multiple threads call the setter at the same time, it will not appear before a certain thread executes all the statements of the setter.The case where another thread starts to execute the setter is the same as the method with the lock at the beginning and end.

nonatomic:does not guarantee the atomicity of the setter/getter,Data may be problematic in multi-threaded situations.

The above is the details of the ios development we introduce to you. Attribute settings readwrite, readonly, retain, copy, assign, nonatomic, I hope you like it.

  • Previous clipboardjs does not require Flash and does not rely on any JS library for text copying and cutting
  • Next Python generative send () method (detailed)