iOS array Nsarray and Nsmutablearray knowledge points

Source: Internet
Author: User

This article is an array of nsarray and nsmutablearray knowledge points of the summary, mainly some common operations, not some operations see its corresponding documents, the following code part of the use of the third-party plug-in blockskit combination;

An array of a:foundation (Nsarray) is an ordered collection of objects B:nsarray can store only objective-c objects, not the basic data types such as int, float, but Objective-c is compatible with C, Therefore, in the OBJECTIVE-C program, you can still use the C array to store the base data type C:nsarray once created it can no longer be changed, if you want to do the array of increment, delete, change, etc. You need to use Nsarray subclass Nsmutablearray to create an object

1:Nsarray some common operations

Nsarray *array=[Nsarray array]; //Add a single//array=[nsarray arraywithobject:@ "Wujy"]; //Add multipleArray=[nsarray arraywithobjects:@"Cnblogs",@". com", nil]; Nsinteger Arraycount=Array.count; NSLog (@"the current array number of arrays is:%ld", Arraycount); intI=0;  for(ID objinchArray) {NSLog (@"the current%d is%@", I,obj); I++; }        //Common Array OperationsNSString *laststring=[Array lastobject]; NSLog (@"the value of the last object is:%@", laststring); NSString*firststring=[Array firstobject]; NSLog (@"the value of the first object is:%@", firststring); NSString*indexstring=[array Objectatindex:1]; NSLog (@"the second object has a value of:%@", indexstring); Nsinteger Indexint=[array Indexofobject:@"Cnblogs"]; NSLog (@"returns the location of the index:%ld", Indexint); //Convert a string to an arrayNSString *arraystring=@"a,b,c,d"; Nsarray*newarray=[arraystring componentsseparatedbystring:@","];  for(ID objinchNewArray) {NSLog (@"Convert current string to%@", obj); }        //determine if an array has elements    if([NewArray Containsobject:@"C"]) {NSLog (@"element with letter C present"); }    Else{NSLog (@"there is no element of letter C"); }        //Easy to createNsarray *twoarray=[nsarray arraywithobjects:@1,@2,@3,@4,@5, nil]; //iterators traverse reverseobjectenumerator array elements from backward to forward accessNsenumerator *arrayenumerator=[Twoarray Reverseobjectenumerator]; ID obj=Nil;  while(obj=[ArrayEnumerator Nextobject]) {NSLog (@"the current value is:%d", [obj intvalue]); }                    //traversing with the Blockskit pluginNsarray *frarray=[nsarray arraywithobjects:@ (0.2),@(0.5),@(0.9), nil]; [Frarray Bk_each:^(id obj) {NSLog (@"%@", obj);        }]; //match the array item, or null if it does not exist.ID Found=[frarray bk_match:^bool (ID obj) {bool Match= ([obj floatvalue]==0.5)?Yes:no; returnmatch;    }]; NSLog (@"there are values that match:%@", found); ID NotFound=[frarray bk_match:^bool (ID obj) {bool Match= ([obj floatvalue]==0.7)?Yes:no; returnmatch;    }]; NSLog (@"non-conforming value (nil):%@", NotFound); //Filter ArraysNsarray *selectfound=[frarray bk_select:^bool (ID obj) {bool Match= ([obj floatvalue]>0.3)?Yes:no; returnmatch;    }]; NSLog (@"the number of currently filtered array arrays is:%ld", Selectfound.count);  for(ID objinchselectfound) {NSLog (@"Current compliance for%@", obj); }        //filter Array to reverseNsarray *rejectfound=[frarray bk_reject:^bool (ID obj) {bool Match= ([obj floatvalue]>0.3)?Yes:no; returnmatch;    }]; NSLog (@"the number of currently-deserialized array arrays is:%ld", Rejectfound.count);  for(ID objinchrejectfound) {NSLog (@"Current compliance for (0.2 compliant)%@", obj); }        //iterate through an array to modify each value to make up a new arrayNsarray *mapfound = [Array bk_map:^ID (id obj) {return[obj stringbyappendingstring:@". PNG"];    }];  for(ID objinchmapfound) {NSLog (@"Current Map%@", obj); }        //merging the values of an array//The merging of the arrays can be done with the addition of a delimiter (this is the one in front |; The merged string is: |cnblogs.com)NSString *concentrated = [Array bk_reduce:@"|"withblock:^ID (ID sum, id obj) {return[sum stringbyappendingstring:obj];    }]; NSLog (@"the merged string is:%@", concentrated); //add another one for the integer type Bk_reduceinteger    floatValue = [Frarray bk_reducefloat:0withblock:^cgfloat (cgfloat result, id obj) {returnResult +[obj Floatvalue];    }]; NSLog (@) After the combined Frarray value is:%f", value);

2:Nsmutablearray some common operations

//transfer from array to NsmutablearrayNsarray *array=[[nsarray Alloc] Initwithobjects:@"wujy",@"Cnblogs", nil]; Nsmutablearray*mutablearray=[Nsmutablearray Arraywitharray:array]; //Create NsmutablearrayNsmutablearray *newmutablearray=[Nsmutablearray array]; [Newmutablearray AddObject:@"a"]; [Newmutablearray Addobjectsfromarray:[[nsarray alloc] initwithobjects:@"b",@"C",@"D",@"e", Nil]]; //iterators traverse reverseobjectenumerator array elements from backward to forward accessNsenumerator *arrayenumerator=[Newmutablearray Reverseobjectenumerator]; ID obj=Nil;  while(obj=[ArrayEnumerator Nextobject]) {NSLog (@"the current value is:%@", obj); }        //Create NsmutablearrayNsmutablearray *capacityarray=[[nsmutablearray Alloc] initwithcapacity:5]; [Capacityarray Addobjectsfromarray:[[nsarray alloc] initwithobjects:@1,@2,@3, Nil]]; NSLog (@"before deletion (%@):", Capacityarray); //deletes the specified element[Capacityarray removeobject:@3]; NSLog (@"after deletion:%@", Capacityarray); //Insert[Capacityarray insertobject:@4Atindex:2]; NSLog (@"post-insertion (1,2,4)%@", Capacityarray); //Insert MultipleNsindexset *Set=[nsindexset Indexsetwithindexesinrange:nsmakerange (0,2)]; [Capacityarray Insertobjects:[[nsarray alloc] initwithobjects:@5,@6, Nil] Atindexes:Set]; NSLog (@"Insert multiple later (5,6,1,2,4):%@", Capacityarray); //blocks Operation//Filter[Capacityarray bk_performselect:^bool (ID obj) {bool Match= ([obj intvalue]>2)?Yes:no; returnmatch;    }]; NSLog (@"the filtered array value is (5,6,4):%@", Capacityarray); //Take reverse filter[Capacityarray bk_performreject:^bool (ID obj) {bool Match= ([obj intvalue]>5)?Yes:no; returnmatch;    }]; NSLog (@"the filtered inverse array value is (5,4):%@", Capacityarray); //manipulating the value of an array[Capacityarray bk_performmap:^ID (id obj) {return[NSNumber numberwithint:[obj intvalue]+1];    }]; NSLog (@"the array value after map is (6,5):%@", Capacityarray);

iOS array Nsarray and Nsmutablearray knowledge points

Related Article

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.