Variable array of SNAIL-OC learning Nsmutablearray

Source: Internet
Author: User

Variable group Nsmutablearray all methods that inherit from Nsarray Nsarray apply to mutable arrays

Here are some initialization methods for mutable arrays

#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool {//variable        Groups inherit from immutable arrays//NULL variable array nsmutablearray * array = [[Nsmutablearray alloc] init];        Incoming 0 is a new empty array nsmutablearray * array2 = [[Nsmutablearray alloc] initwithcapacity:0];                Creates a new array with an array Nsmutablearray * array3 = [[Nsmutablearray alloc] initwitharray:array];                Initialize Add object nil is a placeholder for nsmutablearray * Array4 = [[Nsmutablearray alloc] initwithobjects:@ "DSA" @ "SD", nil];                The simple initialization method is only applicable to non-variable groups//nsmutablearray * array5 = @[@ "DSA" @ "SD"];        The class method form creates an array Nsmutablearray * arr1 = [Nsmutablearray array];        Nsmutablearray * arr2 = [Nsmutablearray Arraywitharray:array];        A variable array of length 0 Nsmutablearray * ARR3 = [Nsmutablearray arraywithcapacity:0];            Nsmutablearray * ARR4 = [Nsmutablearray arraywithobjects:@ "SDA", @ "ASD", nil]; } return 0;}

Similar to a mutable array, a variable group can have a corresponding method for the elements inside it.

#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {    @autoreleasepool {                Nsmutablearray * array = [[Nsmutablearray alloc] init];                Add an element        [array addobject:@ "one1"];        Nsarray * array2 = @[@ "SDA" @ "sad" @ "one"];        Add multiple elements        [array addobjectsfromarray:array2];                Delete all objects with the same parameters        [array removeobject:@ "one"];        Deletes the element at the specified location        [array removeobjectatindex:0];        Delete all elements        //If an empty array call below this method data will be error        [array removeallobjects];                Delete the last element, but the empty array does not error        [array removelastobject];        Delete multiple elements        [array removeobjectsinarray:array2];                Starting with the subscript, remove several elements  from subscript 0 to delete the two        nsrange range = nsmakerange (0, 2);        [Array removeobjectsinrange:range];            }    return 0;}

Some methods for elements of a variable group

#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {    @autoreleasepool {                Nsmutablearray * array = [[Nsmutablearray alloc] init];               The reset array parameter can be any array        [array setarray:@[@ "Snail", @ "Daodao", @ "Jian"];                Replace the element labeled 1 with the @ "Jianjian"        [array replaceobjectatindex:1 withobject:@ "Jian"];                Swap the element with subscript 1 followed by the element labeled 2        [array exchangeobjectatindex:1 withobjectatindex:2];                Inserting subscript 2 will insert a new element in the order of other elements to move backwards        [Array insertobject:@ "J" atindex:2];            }    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Variable array of SNAIL-OC learning Nsmutablearray

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.