4. What is the problem with this notation: @property (copy) Nsmutablearray *array;
Two problems:1, add, delete, change the elements in the array, the program will be unable to find the corresponding method and crash. Since copy is an immutable Nsarray object;
2, using the atomic attribute will seriously affect performance;
1th of the relevant reasons in the following is discussed in the @property declaration nsstring (or nsarray,nsdictionary) often use copy keyword, why? What might be the problem if you switch to Strongkeyword? "And how to use copy keyword?" "is also discussed.
For example, the following code will crash
. h file
http://weibo.com/luohanchenyilong/
Https://github.com/ChenYilong
The following code will crash
@property (nonatomic, copy) Nsmutablearray *mutablearray;
. m file
http://weibo.com/luohanchenyilong/
Https://github.com/ChenYilong
The following code will crash
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@1,@2,nil];
Self.mutablearray = array;
[Self.mutablearray removeobjectatindex:0];
And then it's going to collapse:
-[__nsarrayi Removeobjectatindex:]: Unrecognized selector sent to instance 0x7fcd1bc30460
2nd reason. For example, the following:
This property uses a synchronous lock (atomic). Will generate some extra code at the time of creation to help write multithreaded programs, which can lead to performance issues, by declaring that nonatomic can save these, albeit very small, without additional overhead.
By default, the method synthesized by the compiler ensures its atomicity (atomicity) through the locking mechanism.
If the attribute has a nonatomic trait, no synchronization lock is used. Note that although there is no trait named "Atomic" (assuming that a property does not have a nonatomic trait, it is "Atomic" (atomic)).
in iOS development. You will find. Almost all properties are declared as nonatomic.
Under normal circumstances, the attribute must not be "atomic", since this does not guarantee "thread safety". To implement thread-safe operations, you need to use a deeper locking mechanism. For example, when a thread reads a property value several times in a row and another thread overwrites the value at the same time, it will read a different property value even if the property is declared as atomic.
So. The Nonatomic property is generally used when developing iOS programs. However, when developing a MAC OS X program, using the atomic attribute usually does not have a performance bottleneck.
iOS Learning (3)