iOS Collection Summary

Source: Internet
Author: User

Nsarray for an ordered set of objects (equivalent to an array)

Nsset for object unordered collection

nsdictionary for key-value mappings the above three collection classes are immutable (once initialized, you can no longer dynamically add or remove elements), and if you need to use a mutable collection, you need to use the following classes:

Nsmutablearray

Nsmutableset

Nsmutabledictionary

Example Demo:

1:nsarray Initialization and traversal iterations

(1Nsarray initialization function of Nsarray:-(ID) Initwithobjects: (ID) Firstobject, ....; Nsarray's handy constructor:+(ID) Arraywithobjects: (ID) Firstobject, ...; Demo://convenient initialization of the NsarrayNsarray*array1 = [[Nsarray alloc] Initwithobjects:@"AAA",@"BBB",@"CCC", nil]; //Nsarray's Handy builderNsarray*array2 = [Nsarray arraywithobjects:@"111",@"222",@"333", nil]; (2gets the number of array elements and arrays of elements//get the number and elements of an array element           intCount = (int) [array1 Count]; //gets the element of the corresponding index          IDelement = [Array1 Objectatindex:0]; NSLog (@"Array1_count =%d, array[0] =%@", count, Element); (3) Iteration://encapsulates a function that iterates through an array      voidArray_display (IDArray) {        for(inti =0; i < [array count]; i++) {          IDtemp =[Array objectatindex:i]; NSLog (@"%@", temp); }    }
View Code

2:nsmutablearray Initialization and traversal iterations

 2. Variable array: The capacity of the Nsmutablearray Nsarray is fixed, and the capacity of Nsmutablearray is variable, we can initialize a capacity when the Nsmutablearray is instantiated, but this capacity is not fixed, When it's not enough, it automatically increases. Nsmutablearray is a subclass of Nsarray, an extension of nsarray. (1Nsmutablearray initialization function of Nsmutablearray:-(ID) Initwithcapacity: (Nsuinteger) NumItems; Nsmutablearray's handy constructor:+(ID) Arraywithcapacity: (Nsuinteger) NumItems; Demo://convenient initialization of the NsmutablearrayNsmutablearray *array3 = [[Nsmutablearray alloc] Initwithcapacity:3]; //Nsmutablearray's Handy builderNsmutablearray *array4 = [Nsmutablearray arraywithcapacity:3]; (2elements added and removed add elements:-(void) AddObject: (ID) AnObject; --add elements to the end of the array to delete the entire contents:-(void) removeallobjects; Delete the last element:-(void) Removelastobject; To delete an element by index:-(void) Removeobjectatindex: (Nsuinteger) index; Delete any one of the elements:-(void) Removeobject: (ID)Object; Demo://Initialize NsmutablearrayNsmutablearray *array = [Nsmutablearray arraywithobjects:@"111",@"222",@"333", nil];//adding elements[Array AddObject:@"444"]; //remove Element[Array Removeobject:@"111"]; //remove the last element[Array removelastobject];//Delete an element by index[Array Removeobjectatindex:0];  Array_display (array); Delete any one of the elements:-(void) Removeobject: (ID)Object;//Initialize NsmutablearrayNsmutablearray *array = [Nsmutablearray arraywithobjects:@"111",@"222",@"333", nil];//adding elements[Array AddObject:@"444"]; //remove Element[Array Removeobject:@"111"]; //remove the last element[Array removelastobject];//Delete an element by index[Array Removeobjectatindex:0]; Array_display (array);
View Code

iOS Collection Summary

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.