All operations for iOS mutable arrays

Source: Internet
Author: User

#pragma mark Create array C

Nsmutablearray * Array =[[nsmutablearray alloc] initwithobjects:@ "a", @ "B", @ "C", nil];

Nsmutablearray * array1 =[nsmutablearray arraywithobjects:@ "a", @ "B", @ "C", nil];

Nsarray * array2 = @[@ "1", @ "2", @ "3"];

Nsmutablearray * Array3 = [[Nsmutablearray alloc]initwitharray:array2];

NSLog (@ "%@", array3);

#pragma mark variable array add element

[Array addobject:@ "D"];

[Array addobject:@ "E"];

NSLog (@ "%@", array);

#pragma mark variable array is incremented within the specified range

Array Current Range

[Array insertobject:@ "F" atindex:0];

NSLog (@ "%@", array);

#pragma mark appends an array to the array

Nsarray * Array4 = @[@ "G", @ "H", @ "I"];

[Array Addobjectsfromarray:array4];

NSLog (@ "%@", array);

#pragma mark replaces the elements in the array

[Array1 replaceobjectatindex:2 withobject:@ "HHH"];

NSLog (@ "%@", array1);

#pragma mark swaps the positions of elements in an array

[Array exchangeobjectatindex:0 withobjectatindex:5];

NSLog (@ "%@", array);

#pragma mark is removed from the array

1. Delete the last element

[Array Removelastobject];

NSLog (@ "%@", array);

2. Delete the element with the specified subscript

[Array removeobjectatindex:5];

NSLog (@ "%@", array);

3. Delete elements directly

[Array removeobject:@ "E"];

NSLog (@ "%@", array);

4. Remove elements within a specified range

In this range, if this element is present, delete the

If it doesn't exist, do not handle it.

0 position

5 length

Nsrange range = {0,5};

[Array removeobject:@ "G" inrange:range];

NSLog (@ "%@", array);

Remove from an array

Nsarray * arr [email protected][@ "A", @ "B", @ "C", @ "D"];

[Array Removeobjectsinarray:arr];

NSLog (@ "%@", array);

6 deletes all the elements in the array, but the array still exists, but there are no elements.

[Array removeallobjects];

NSLog (@ "array =%@", array);

#pragma mark modifies the array

[Array Setarray:arr];

NSLog (@ "%@", array);

All operations for iOS mutable arrays

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.