Nsarray : Non-variable group
Nsmutablearray: variable Array
C-language arrays: storing basic data types
OC array: Can only hold OC objects, cannot hold non-OC object types, such as int, struct, enum, etc.
1. Creation of immutable variable groups
voidCreate () {/*int a = 5; int ages[10] = {1, 90, 89, 17}; C-array person *p = [[Person alloc] init]; OC Array (below) person *persons[5] = {p, [[Person alloc] init]}; */ //OC Array cannot hold nil value//OC Arrays can only hold OC objects, cannot hold non-OC object types, such as int, struct, enum, etc.//This array is always an empty array//Nsarray *array = [Nsarray array]; /*creation of 1.NSArray*/Nsarray*array2 = [Nsarray arraywithobject:@"Jack"]; //Nil is the tag at the end of the array elementNsarray *array3 = [Nsarray arraywithobjects:@"Jack",@"Rose", nil]; //[array2 Count]; //Nsarray *array4 = [Nsarray arraywithobjects:@ "Jack", @ "Rose", @ "4324324", nil]; //quickly create a Nsarray object (this method is used only to create immutable groups)Nsarray *array4 = @[@"Jack",@"Rose",@"4324324"]; /*number of elements in 2.NSArray*/NSLog (@"%ld", Array3.count); /*access to elements in 3.NSArray*/NSLog (@"%@", [Array3 Objectatindex:1]); //array3[1];NSLog (@"%@", array3[0]);}2. Non-variable group traversal
//iterating through an arrayvoiduse1 () { person*p =[[Person alloc] init]; Nsarray*array = @[p,@"Rose",@"Jack"]; //Method 1 (by subscript)//for (int i = 0; i<array.count; i++)// { //NSLog (@ "%@", Array[i]); // } // Method 2 (for in structure) --Recommended use //ID obj represents each element in an array//int i = 0; //For (ID obj in array)// { // //find the position of the obj element in the array//Nsuinteger i = [array indexofobject:obj]; // //NSLog (@ "%ld-%@", I, obj); // //i++; // //if (i==1)// { //Break ; // } // } // Method 3:block --Recommended use //each time you traverse to an element, the block is called//and the current element and index position are passed as parameters to block[Array Enumerateobjectsusingblock:^(IDobj, Nsuinteger idx, BOOL *stop) {NSLog (@"%ld-%@", idx, obj); if(idx = =0) { //Stop Traversal*stop =YES; } }]; //the nature of the block above//void ^ (Myblock) (ID, Nsuinteger, bool *) = ^ (Id obj, Nsuinteger idx, BOOL *stop)// { //NSLog (@ "%ld-%@", idx, obj); // // //if (idx = = 0)// { // //Stop Traversal//*stop = YES; // } // }; // //for (int i = 0; i<array.count; i++)// { // //used to mark whether or not to stop traversal//BOOL isstop = NO; // // //remove Element//id obj = array[i]; // //Myblock (obj, I, &isstop); // // //if (isstop)// { //Break ; // } // }}3. Use of non-variable groups
voidUse2 () {Nsmutablearray*array = [Nsmutablearray arraywithobjects:@"Rose",@"Jim", nil]; //adding elements[Array Addobject:[[person alloc] init]; [Array AddObject:@"Jack"]; //Delete Element//[Array removeallobjects]; //deletes the specified object//[Array removeobject:@ "Jack"];[Array Removeobjectatindex:0]; //wrong wording//[Array Addobject:nil];NSLog (@"%@", array); NSLog (@"%ld", Array.count);}4. Variable array
Nsmutablearray its use of basic and non-variable groups similar----specific reference to the document, there are many methods, the document description is kingly.
Attention:
int Main () { // @[] creates only immutable group Nsarray/ * error Nsmutablearray *array = @ [@ "Jack" @ "Rose"]; [Array addobject:@ "Jim"]; */ // Nsarray *array = @[@ "Jack" @ "Rose"]; return 0 ;}
iOS development OC (18)--foundation (3) Nsarray and Nsmutablearray