iOS Learning (3)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.