In the Cocoa Foundation, Nsarray and Nsmutablearray are used for an ordered set of objects, the biggest difference between Nsarray and Nsmutablearray classes is that Nsarray is immutable and Nsmutablearray is mutable. They can only store cocoa objects (NSObject objects), if you want to save some raw C data (such as: Int,float,double,bool, etc.), you need to encapsulate these raw C data nsnumber type, their subscript is starting from 0, Here are some common primary operations for the Nsarray and Nsmutablearray classes.
1.NSArray initialization
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "Superdo.horse", @ "Superdo.mount", @ "Superdo.ac", nil];// Initialize with an existing array nsarray *array1 = [Nsarray Arraywitharray:array];
2.NSArray Quick Enumeration
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "Superdo.horse", @ "Superdo.mount", @ "Superdo.ac", Nil];for (NSString *str in array) { NSLog (@ "%@", str);}
3.NSMutableArray Simple Sorting
Nsmutablearray*array = [[Nsmutablearray alloc] initwithobjects:@ "Superdo.horse", @ "Superdo.mount", @ "SuperDo.AC", nil The elements in the];//array are sorted by string size: [Array sortusingselector: @selector (compare:)]; NSLog (@ "sorted array:%@", array);
4. String---> Nsarray
NSString *string = [[NSString alloc] initwithstring:@ "a| b| c| D "]; NSLog (@ "string:%@", string); Nsarray *array = [string componentsseparatedbystring:@ "|"]; NSLog (@ "array:%@", array);
5.NSArray---> Strings
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "A", @ "B", @ "C", @ "D", nil]; NSString *string = [Array componentsjoinedbystring:@ "|"]; NSLog (@ "string:%@", string);
6. Element manipulation
Insert element Nsmutablearray *array = [Nsmutablearray arraywithobjects: @ "One", @ "one", @ "three", nil];[ Array addobject:@ "four"; NSLog (@ "array:%@", array);//delete element [array removeobjectatindex:1]; NSLog (@ "array:%@", array);//enumeration element (formerly Backward) nsenumerator *enumerator = [Array Objectenumerator];id next;while (next = [ Enumerator Nextobject]) { NSLog (@ "Object------":%@ ", next);} enumeration element (from backward forward) nsenumerator *enumerator = [Array Reverseobjectenumerator];id object;while (object = [Enumerator Nextobject ] { NSLog (@ "Object------":%@ ", object);}
This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4594178.html
[Objective-c] 008_foundation framework of Nsarray and Nsmutablearray