Original URL: http://www.jianshu.com/p/1ad327f56d1d
Non-variable group Nsarray
Create an empty arrayNsarray *array = [Nsarray array];There is no point in creating this.Create an array of only one elementNsarray *array1 = [Nsarray Arraywitharray:@ "a"];Nsarray *array2 = [Nsarray Arraywithobject:@ "B"];Creating multiple element arrays with the convenience builderNsarray *array3 = [Nsarray arraywithobjects:@ "Hola",@ "Bonjour",@ "Guten",@ "Tag", nil]; //note finally nil //create an array with multiple elements using object method nsarray alloc] initwithobjects:@ "1", @ "2", @ "3", @ "4", nil]; //create another array with one array nsarray *array5 = [ nsarray arraywitharray:array3]; //all objects in the console print array using the array name nslog (@ " %@ ", array5); //an object in the console print array using the array name + subscript method nslog ( @ "%@", Array5[0]);
Some common uses:
Nsarray *array = [Nsarray arraywithobjects:@ "Hola",@ "Guten",@ "Tag",@ "Bonjour",NIL];1. Get the number of elements in an array using countNSLog (@ "%ld", [array Count]); //2. Gets the object NSLog (@ "%@", [array Objectatindex:2]) corresponding to the subscript according to the subscript; //Here [] cannot be used instead of//3. Find subscript by element Nsuinteger r = [Array indexofobject:@ "three"]; NSLog (@ "%ld", R); //4. Determine if an element is contained in the array BOOL r1 = [array containsobject:@ "one"]; if (R1) { NSLog (@ "contains this element!");} else{ NSLog (@ "not found!");}
Simplified method of Creation (syntax sugar):
NSArray *array = @[@"one",@"two",@"three",@"four"];
Simplified access (accessed by element subscript)
NSString *str = array[2]; NSLog(@"%@",str);
Array Traversal:
The first way: normal traversal--accessed through the following tablefor (int i =0; I < array.count; i++) {nslog (@ "%@", Array[i]);} //the second Way: Fast enumeration method--for cycle of the enhanced version for (in array) {nslog (@ " %@ ", obj); } //the Third Way: Traverse with block [array enumerateobjectsusingblock:^ (nsuinteger idx, bool * _nonnull stop) {if (idx = = 2) {//idx is subscript *stop = yes; }else{nslog (@ "subscript as:%ld object is%@", Idx,obj); } }];
Variable group Nsmutablearray
Variable group Nsmutablearray inherit from immutable group Nsarray
//1. Create an empty array Nsmutablearray *array =[nsmutablearray array]; //2. Dimension array append element [array addobject:@1 ]; //3. Creating an array, adding multiple elements nsmutablearray *array2 = [Nsmutablearray arraywithcapacity:5"; //add 5 Elements [array2 addobject:@1]; array2[ 1] = @2; Array2[2] = @3; Array2[3] = @4; Array2[4] = @5; //output array NSLog (@ "%@", array2);
There are some operations that can be variable groups that do not have a variable group:
inserting insert [Array2 insertobject:@6 Atindex:0];NSLog (@ "%@", array2);Replacing replace replace [array2 Replaceobjectatindex:0 withobject:@0]; nslog (@ "%@", array2); //delete remove [array2 removelastobject]; nslog (@ "%@", array2); //contains contain bool r = [Array2 Containsobject:@2"; //This method has a return value if (r) {NSLog (@ "contains!"); else {nslog (@ "not included!");} //exchange elements Exchange [Array2 exchangeobjectatindex:0 Withobjectatindex:2]; nslog (@ "%@", array2);
A typical error usage:
// NSMutableArray *array =@[@1,@2,@3,@4,@5];// [array addObject:@"hello"];// NSLog(@"%@",array3);
Note: Syntactic sugars are created for immutable objects, and if you create a Mutable object, use the syntax sugar, it will be reported yellow, after creation, and then add objects for this space, the program throws an exception, because at run time, the default is an immutable array.
Please call me the Holy Sage (Jane book author)
Original link: http://www.jianshu.com/p/1ad327f56d1d
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
"Go" immutable array nsarray and variable arrays Nsmutablearray