Multiple cases in iOS caused by the deviation in understanding the use of arrays NSArray and NSMutableArray

Source: Internet
Author: User

This simple question has always been told by five years of Apple development experience that it is not worth mentioning and there is nothing to mention.

However, today I have made a mistake that is not worth mentioning. For a long time, I think it is not worth mentioning. It is also a small mistake that is caused by the strangeness of my understanding.

The question is: traverse an array. If the conditions are met, add another array and remove it from the array to save storage space.

Analysis:

1. Because the addition and subtraction of the same object between arrays, this is only adding and dropping pointers. It only increases the reference count and does not increase the actual memory space allocation;

2. There is no problem with adding. The declared target array is NSMutableArray. When I subtract it, I use the delete method, but this method does exist;

Statement

NSArray *rows = [fileContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];NSMutableArray *geometryRows = [[NSMutableArray alloc] init];
Use

[geometryRows addObject:row];
[rows delete:row];
This error is made by using UIResponder and the delete method of this class is automatically prompted;

In fact, rows is NSArray and its elements cannot be edited. Just change them to the following:

    NSMutableArray *rows = [NSMutableArray arrayWithArray:[fileContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];

However, at present, this question is just a question for finding a problem. Why? The reason is as follows:

1. Increasing the number is also to increase the reference count and pointer address of the original object. There will be no memory allocation for new copies of the original object content. Therefore, the attached memory allocation is very small and you do not need to consider it;

2. If the declared pointer does not exist and the content pointed to by the original pointer does not have a pointer index, the ARC will detect it during compilation, add the code for releasing memory for it, so there is no need to consider Memory leakage;

3. From this we can see that we have been worried about nothing, and it has caused so many cases of blood and wasted so much publicity. We hope that we can also make a difference to watch the world, do not commit multiple cases that result from a series of misunderstandings;


Click here!



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.