Why can't copy or nsmutablecopy be used for NSMutable attribute declaration?

Source: Internet
Author: User

Why can't copy or nsmutablecopy be used for NSMutable attribute declaration?

During iOS development, we often declare NSMutable (variable classes, such as NSMutableString, NSMutableArray, NSMutableDictionary, and NSMutableData) attributes, during the Declaration, we all know that strong (strong reference) is used for identification, but many people do not know why copy cannot be used for identification. We will discuss it below:

1. Why can't I use copy:

In general, all variable classes are inherited from non-variable classes and belong to the subclass of variable classes. Take the NSMutableString class as an example and you will enter the NSMutableString class. the hfile can be seen that it inherits from the NSString class, And the NSMutableString class does not override the copy method of the NSString class. Therefore, if we declare the NSMutableString class attribute, we use copy for identification, when we assign values to this attribute, we call the copy method of the NSString class. The instance object we get is actually an NSString instance, not an NSMutableString instance, the following code is used for verification:

 

The output result shows that tempStr is an NSMutableString object. The attribute 'str' declared by strong is only a reference count added to the tempStr object, and no new object instance is generated, therefore, the class methods of tempStr and str actually call the methods of the same object, so the output results are the same. When the attribute mstr declared by using copy is assigned a value, the copy method of the tempStr object is called to generate a new object. The output result shows that this object is an NSString object.

In summary, the NSMutable attribute declaration cannot be modified using copy because the NSMutable class does not override the copy method of the non-variable class. When assigning values to the attribute, the copy method of the parent class is called, the obtained object is a mutable object.

2. What if copy is used:

Because the object obtained when an object is assigned a value is a non-mutable object, the program will crash when we call a special method of the variable object using this attribute (because the object cannot respond to this method at all ), the following code also applies to Verification:

The replaceCharactersInRange method of the tempStr object and str attribute is successfully executed, but the program runs to [self. mstr replaceCharactersInRange: NSMakeRange (0, 1) withString: @ ""] The Code crashes, so this problem has a great impact on the program, this bug is hard to be found, so pay more attention when declaring the NSMutable attribute.

The first time I wrote a blog, I hope it will be helpful to you. What are the shortcomings in this blog? I hope you can help me to point out that you can share your ideas and make progress together.

 

Related Article

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.