Objective-c Learning-Arrays Nsarray and Nsmutablearray (welcome suggestion)

Source: Internet
Author: User

  In the process of learning objective-c, the sensory array is a relatively large application domain in this learning aspect, so write this essay to summarize.

First of all, unlike the C language array, objective-c is the array can only be used to store objects, instead of storing int, double, char and other basic data types, generally to save these basic data types, for I just learned objective-c novice, My approach is to store the underlying type of the number, such as int, double, and Char, first converted to a class char* type called NSNumber, converted to objective-c string NSString class for storage, related NSNumber and Nsstri Ng in the future when I learned to summarize my study results.

The objective-c array is divided into two types, one called an immutable array (Nsarray), one is called a mutable (Nsmutablearray), and the Nsmutablearray class is a subclass of the Nsarray class.

(1) Immutable Array (Nsarray)---------The type is not changed after it is determined, and the interchange

Initialize------(two ways)

Nsarray * array =[[Nsarray alloc] initwithobjects:@ "abc", @ "123", @ "abc", nil];

Nsarray * array = @[@ "abc", @ "123", @ "abc"]; I later saw the teacher define the array in this way to initialize, so also recorded down

  (2) variable group (Nsmutablearray)-------The type is determined, the element can be added, deleted, changed (including the position of the element and the number of elements of the array)

//Initialize------(two ways)

Nsmutablearray * Mutablearray = [[Nsmutablearray alloc] init];

Nsmutablearray * Mutablearray = [Nsmutablearray array]; Teach my teacher to show when the initialization of the two types of initialization there is no difference, or different, now do not discuss

    

    add element

For mutable arrays, Apple gives us a lot of ways, because for starters, I've basically used one of these      

[Mutablearray AddObject: (ID)]; This is a way for Mutablearray object calls to add elements

For example: [Mutablearray addobject:@ "EFG"]; This is an element with a nsstring type added to the Mutablearray object with a value of @ "EFG"

     

Delete Element

[Mutablearray removeallobjects]; This method is to remove all elements from the Mutablearray

[Mutablearray Removelastobject]; The method is to remove the last element of the Mutablearray

[Mutablearray Removeobject: (ID)]; This method removes the element with the ID (which is actually incorrect), and the ID actually holds the address, except for the string

For example: [Mutablearray removeobject:@ "abc"]; This is an element in the Mutablearray object that has a value of "ABC" removed

[Mutablearray Removeobjectatindex: (Nsuinteger)]; This method removes the element with the position nsuinteger, which is actually the deletion of an element similar to the subscript Nsuinteger

For example: [Mutablearray removeobjectatindex:3]; This is the deletion of the 4th element

Modify element position

[Mutablearray Exchangeobjectatindex: (NSUInteger1) Withobjectatindex: (NSUINTEGER2)]//This method is to position the elements of NSUInteger1 and NSUInteger2 elements to swap positions

For example: [Mutablearray exchangeobjectatindex:1 withobjectatindex:2];//means exchanging the 2nd element and the 3rd element (because Nsuinteger is subscript)

/*****

Array traversal (I know there are currently three ways)

****/

1, Enumerator method (this method can not change the number of arrays and the position of the element in the process of traversal, or error)

Nsenumerator * Enumerator = [Mutablearray objectenumerator];//construct Enumerator

ID obj;

while (obj = [Enumerator nextobject]) {

obj is both an element

}

2, Fast enumeration method (some are called Fast for loop) (this method still cannot change the number of arrays and the position of the elements in the process of traversal, otherwise it will be wrong)

for (id obj in Mutablearray) {

obj is both an element

}//This method they say is the fastest, I also as the fastest to use

For enumerators, there is also an enumerator called reverse order, as follows

Nsenumerator *enumrator = [Mutablearray reverseobjectenumerator];//constructs a reverse-order enumerator, using the same enumerator, but not the same as when iterating with the reverse enumerator, Both of these methods can change the number of arrays and the position of the elements.

3. Use for loop traversal (this traversal method can change the number of arrays and the position of the elements in the loop)

for (int i = 0; i < [Mutablearray count]; i++) {

ID obj = [mutablearray objectatindex:i];//obj is both an element

}

Objective-c Learning-Arrays Nsarray and Nsmutablearray (welcome suggestion)

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.