Introduction to the Nsarray cluster of iOS-(copy, mutablecopy causes program crash)

Source: Internet
Author: User

1. Preface

The development time often uses the array to process the data, the operation to the Nsmutablearray often causes the program to crash, the special research Nsarray's class cluster! Involving __nsplaceholderarray, __nsarray0, __nssingleobjectarrayi, __nsarrayi, __nsarraym related classes.

2, Analysis Nsarray

2.1. Create an immutable lease

Nsarray *placeholder =[Nsarray alloc]; Nsarray*ARR1 =[placeholder Init]; Nsarray*ARR2 = [Placeholder initwithobjects:@0, nil]; Nsarray*ARR3 = [Placeholder initwithobjects:@0, @1, nil]; NSLog (@"arr:%s", Object_getclassname ([Nsarray array]);//arr: __nsarray0NSLog (@"placeholder:%s", Object_getclassname (placeholder));//placeholder: __nsplaceholderarrayNSLog (@"arr1:%s", Object_getclassname (arr1));//arr1: __nsarray0NSLog (@"arr2:%s", Object_getclassname (ARR2));//arr2: __nssingleobjectarrayiNSLog (@"ARR3:%s", Object_getclassname (ARR3));//ARR3: __nsarrayi

You can see [Nsarray array] is equivalent to [[Nsarray alloc] init], are empty element class __nsarray0;

__nsplaceholderarray:alloc objects are unified for this class object, and the immutable array is the same;

__nsarray0: There are no elements after the array init;

__nssingleobjectarrayi: The array has only one element;

__nsarrayi: Non-variable group cut elements in more than one;

2.2, respectively to ARR1, ARR2, ARR3 copy and mutablecopy operation:

NSLog (@"arr1:%s", Object_getclassname ([arr1 copy]);//arr1: __nsarray0NSLog (@"arr2:%s", Object_getclassname ([arr2 copy]);//arr2: __nssingleobjectarrayiNSLog (@"ARR3:%s", Object_getclassname ([arr3 copy]);//ARR3: __nsarrayiNSLog (@"================="); NSLog (@"arr1:%s", Object_getclassname ([arr1 mutablecopy]);//arr1: __nsarraymNSLog (@"arr2:%s", Object_getclassname ([arr2 mutablecopy]);//arr2: __nsarraymNSLog (@"ARR3:%s", Object_getclassname ([Arr3 mutablecopy]);//ARR3: __nsarraym

A copy of an immutable group does not change the class name, but the mutablecopy becomes a mutable array;

3, Analysis Nsmutablearray

3.1. Create an immutable variable group

Nsmutablearray *arr =[Nsmutablearray alloc]; Nsmutablearray*ARR1 =[arr init]; Nsmutablearray*ARR2 = [Arr initwithobjects:@0, nil]; Nsmutablearray*ARR3 = [Arr initwithobjects:@0, @1, nil]; Nsmutablearray*ARR4 = [Arr initwithobjects:@0, @1, @2, nil]; NSLog (@"arr:%s", Object_getclassname ([Nsmutablearray array]);//arr: __nsarraymNSLog (@"placeholder:%s", Object_getclassname (arr));//placeholder: __nsplaceholderarrayNSLog (@"arr1:%s", Object_getclassname (arr1));//arr1: __nsarraymNSLog (@"arr2:%s", Object_getclassname (ARR2));//arr2: __nsarraymNSLog (@"ARR3:%s", Object_getclassname (ARR3));//ARR3: __nsarraym

It can be seen that [Nsmutablearray array] is equivalent to [[Nsmutablearray alloc] init], which is a variable array class __nsarraym;

The object of __nsplaceholderarray:alloc is first unified for this class object;

__NSARRAYM: Variable array class;

3.2, respectively to ARR1, ARR2, ARR3 copy and mutablecopy operation:

NSLog (@"arr1:%s", Object_getclassname ([arr1 copy]);//arr1: __nsarray0NSLog (@"arr2:%s", Object_getclassname ([arr2 copy]);//arr2: __nssingleobjectarrayiNSLog (@"ARR3:%s", Object_getclassname ([arr3 copy]);//ARR3: __nsarrayiNSLog (@"================="); NSLog (@"arr1:%s", Object_getclassname ([arr1 mutablecopy]);//arr1: __nsarraymNSLog (@"arr2:%s", Object_getclassname ([arr2 mutablecopy]);//arr2: __nsarraymNSLog (@"ARR3:%s", Object_getclassname ([Arr3 mutablecopy]);//ARR3: __nsarraym

Copy of the immutable group changes to the corresponding immutable group class name, but Mutablecopy does not change the array;

4. Method holding Array

 NSLog (@ " ==%@  "  ,ARR3); NSLog ( @ " ARR3:%p  " , ARR3); //  arr2:0x608000014050      [self EXCHANGEMARR:ARR3]; NSLog ( @ " ==%@      " ,ARR3); NSLog ( @ " ARR3:%p  " , ARR3); //  arr2:0x608000014050 
-(void) Exchangemarr: (Nsmutablearray *) arr{    *arr1 = arr;    [arr1 Removelastobject];    NSLog (@ "arr3:%p", arr1);                   // arr2:0x608000014050}

Results:

2018- .- One  -: the:05.902187+0800arrtest[5672:265513] ==(    0,    1)2018- .- One  -: the:05.902476+0800arrtest[5672:265513] ARR3:0x60400024b1002018- .- One  -: the:05.902828+0800arrtest[5672:265513] ARR3:0x60400024b1002018- .- One  -: the:05.903073+0800arrtest[5672:265513] ==(    0)2018- .- One  -: the:05.903227+0800arrtest[5672:265513] ARR3:0x60400024b100

The address of the array is not changed, and the ARR3 is changed after ARR has been modified in the method;

So if you do not want ARR3 to be affected when processing the data, you need to initialize it or copy it.

5, __nsplaceholderarray simple explanation

The __nsplaceholderarray classes are generated when Nsarray and Nsmutablearray are alloc, and are immutable or mutable arrays only when Init.

This is the case with NSNumber and NSString, which is the design pattern of class clusters.

Introduction to the Nsarray cluster of iOS-(copy, mutablecopy causes program crash)

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.