The number of components in OC is immutable (nsarray) and variable array (nsmutablearray).
Non-variable groups:
1 //------------------------------immutable Groups----------------------------------2 3 //Create a non-variable group4 5NSNumber *number = [NSNumber numberwithint: A];6 7Nsarray * arr= [Nsarray arraywithobjects:@" China",@" One",@"USA", number, nil];8 9NSLog (@"%@", arr);Ten One //array.count Number of elements in an array A -NSLog (@"%@ \ n Array arr element number is%lu", arr, (unsignedLong) arr.count); - the - - //determine if there is an element in the array - +BOOL Yesorno = [arr containsobject:@"USA"]; - +NSLog (@"%d", Yesorno); A at - - //determine if there is an element in the array Method 2 - - if([arr Containsobject:@"USA"]) { - inNSLog (@"there's USA in there."); - to } + - the * //find the position of an element in an array $ Panax NotoginsengNsinteger Indexnow = [arr indexofobject:@"USA"]; - theNSLog (@"%ld",(Long) indexnow); + A the + //find the last element - $ IDLastout =[arr lastobject]; $ -NSLog (@"%@", lastout);
Variable groups:
1Nsmutablearray *marray = [Nsmutablearray arraywithobjects:@" One",@" Both",@"three",@" Four",@"Five",@"Six",@"Seven", nil];2[Marray AddObject:@"Eight"];//append a single element3[Marray InsertObject:@"Zero"Atindex:0];//inserts an element into a specified position4[Marray Removeobjectsinarray:arr];//array of ARR elements deleted in Marray5[Marray Removeobject:@"three"Inrange:nsmakerange (0, Marray.count)];//Delete by Scope6[Marray Removelastobject];//Delete last element7[Marray Removeobject:@"Six"];//Delete a specific element8[Marray Removeobjectatindex:2];//follow the subscript to delete9[Marray Replaceobjectatindex:0Withobject:@"Third"];//replace element with subscriptTen[Marray Exchangeobjectatindex:1Withobjectatindex:2];//Exchange Elements according to subscript OneNSLog (@"%@", Marray);
Traversal of the array:
1 //------------------------------iterate through an array----------------------------------2 3 4Nsmutablearray *marray2 = [Nsmutablearray arraywithobjects:@" One",@" Both",@"three",@" Four",@"Five",@"Six",@"Seven", nil];5 6 //the first method of traversing a mutable array--Fast enumeration method7 for(IDXinchmArray2) {8NSLog (@"%@", x);9 }Ten One //the second method of traversing variable arrays--general cyclic method A for(intI=0; i<marray2.count; i++) { -NSLog (@"%@", [MArray2 objectatindex:i]); - } the - //the third way to traverse a mutable array--using an enumerator to traverse -Nsenumerator *enu =[MArray2 Objectenumerator]; - IDx; + while(x=[ENU nextobject]) { -NSLog (@"%@", x); + } A at - //Create an array new notation @[@ "", @ "" " -Nsarray *array1d = @[@"SDF",@"DF" ]; -NSLog (@"%@", array1d);
iOS Development-objc-nsarray