The two most commonly used containers for storing data in OC are arrays and dictionaries, and as most commonly used, you should understand all the features and usages.
an array in OC is a capacity, an orderly management of a series of elements, and the elements stored in the array, must be the object type.
Immutable groups, see the name of the meaning, immutable, as long as the creation of success can not change the array capacity, elements.
Create an immutable Group object
//1. initialization method (multiple objects are separated by commas, and the last object has a comma between nil)
nsarray *array1 = [[nsarray alloc] initwithobjects:@ "Zhonger",@ " Honghuang ",@" Taixu ",@" Zhili ", Nil]; //nil is not an object, just a flag that tells the compiler that an array assignment ends
//2. Convenience Builder
nsarray *array2 = [nsarray arraywithobjects:@ "Zhuba",@ "Bada",@ " Zhonger ",@" Duliu ", Nil];
The printed data is displayed in () content, such as:
is ( Zhonger, Honghuang, Taixu, Zhili)
//3. laugh, grammar , grammar, sugar, literal . This is the notation of the immutable group ...
Arrays in//oc can hold different types of objects
nsarray *array3 = @[@ "Yousiyi", @ "Huangshenme", @ "Shuishen", @ " Qiuxiang ", @12];
the two core methods of the array are count Objectatindex:
//count to find the number of array elements
unsigned long count = [array1 count];
NSLog(@ "Array1count is%lu", Count); Can print out the number of elements
//objectatindex: The corresponding element is found by the given subscript
nsstring *bada = [array2 objectatindex:1];
NSLog(@ "Bada is%@", Bada);
nsstring *zhonger = array2[2]; How to express the grammatical sugars
NSLog(@ "Zhonger is%@", Zhonger);
// traverse every element inside the array3
unsigned long count1 = [array3 count];
for (int i = 0; i < count1; i + +) {
NSLog(@ "%@", Array3[i]); Grammar sugar
}
// determine if an element is contained in an array
BOOL isTrue = [array2 containsobject:@ "Zhuba"];
NSLog(@ "IsTrue is%d", isTrue);
// Gets the array subscript for an element
Nsuinteger index = [array2 indexofobjectidenticalto:@ "Bada"];
NSLog(@ "index is%lu", index);
// array to implement the split string, stitching strings,
Separating strings with spaces
nsstring *str = @ "Zhonger m";
nsarray *resultarray = [str componentsseparatedbystring:@ ""];
string with 66666 stitching
nsarray *rarray = @[@ "Bada", @ "Qiuxiang",@ "Zaiyiqi"];
nsstring *str1 = [Rarray componentsjoinedbystring:@ "66666"];
As for variable arrays, in the case of non-variable groups, the operation of adding and deleting is provided, so that the array function becomes more diversified .
//1. Create a mutable array
nsmutablearray *marr = [[nsmutablearray alloc]initwithcapacity:0 ];
//2. Convenience Builder
nsmutablearray *marr1 = [nsmutablearray arraywithcapacity:0];
//3. How to create a variable array literal
nsmutablearray *marr2 = [@[@ "Zhonger", @ "Dada", @ "ZhuZhu", @ "Linlin"] mutablecopy];
// add elements to a mutable array,addobject
[MArr2 addobject:@ "AddObject"];
Inserts a new element into a variable array at the specified position insertobject
[MArr2 insertobject:@ "Charu" atindex:1];
// Remove an element from a variable group
// by subscript
[MArr2 removeobjectatindex:4];
NSLog(@ "MARR2 is%@", MARR2);
// by specifying content
[MArr2 removeobject:@ "ZhuZhu"];
NSLog(@ "MARR2 is%@", MARR2);
// Delete the last element in the array
[MARR2 removelastobject];
NSLog(@ "MARR2 is%@", MARR2);
// Delete all elements in the array
//[marr2 removeallobjects];
//nslog (@ "MARR2 is%@", MARR2);
// replace elements in a mutable array
[MArr2 replaceobjectatindex:0 withobject:@ "Gou"];
[MArr2 replaceobjectatindex:1 withobject:@ "Taixu"];
NSLog(@ "MARR2 is%@", MARR2);
// swap position of two elements in a mutable array
[MArr2 exchangeobjectatindex:0 withobjectatindex:1];
NSLog(@ "MARR2 is%@", MARR2);
Note: Whether immutable or mutable arrays, if it is in the MRC environment, as long as the addition of this array, is caused by the reference count plus 1, as one of the three large capacity, the array holds an ordered object, different from the Order of the dictionary, the array can be sorted.
Objective-c Nsarray immutable array and Nsmutablearray variable arrays