iOS Development-objc-nsarray

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.