Nssarry usage in iOS

Source: Internet
Author: User

First, create an array

Create an empty array of  02.NSArray *array = [Nsarray array];      03.//creates an array of 1 elements  04.array = [Nsarray arraywithobject:@ "123"];  05.//creates an array with multiple elements  06.array = [Nsarray arraywithobjects:@ "a", @ "B", @ "C", nil Nil];  07.NSArray *array3 = [Array Arraybyaddingobjectsfromarray:[nsarray arraywithobjects:@ "4", @ "5", nil Nil]];  09.NSArray *array4 = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil Nil];  10.NSRange range = Nsmakerange (1, 2);  11.NSArray *array5 = [Array4 subarraywithrange:range];  

Ii. basic usage of arrays

int count = [array count];//number  02.//determines if an element 03.if is included  ([Array containsobject:@ "a"]) {  .    NSLog (@ "contains string a");  06.NSString *last = [array lastobject]; last element  07.NSString *str = [array objectatindex:1]; Gets the elements in the array according to the index  08.int index = [array indexofobject:@ "C"]; Gets the index of the specified element  09.//let all objects inside the array call the test method, 123 is the parameter  10.NSArray *array = [Nsarray arraywithobjects:stu1, STU2, STU3, nil Nil];  11.[array makeobjectsperformselector: @selector (test2:) withobject:@ "123"];  12.NSArray *array = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", nil Nil];  13.//1-2-3-4  14.//using separators-stitching all array elements  15.NSString *str = [Array componentsjoinedbystring:@ "-"];  16.//writes an array to the file (resulting in an XML file)  17.NSString *path = @ "/users/apple/desktop/array.xml";  18.[array Writetofile:path Atomically:yes];  19.path = @ "/users/apple/desktop/array.txt";  20.//reading array contents from a file (file has strict formatting requirements)  21.NSArray *array2 = [Nsarray Arraywithcontentsoffile:path];  

Third, iterating through the array

#pragma mark traverses the array 1 02.void arrayFor1 () {03.  Nsarray *array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil];  *. int count = Array.count;        The. for (int i = 0; i<count; i++) {06.  ID obj = [array objectatindex:i];  07.} 08.}    #pragma mark traversed the array 2 fast traversal of 11.void ArrayFor2 () {12.  Student *STU1 = [Student Student];  Nsarray *array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil];  int i = 0;        (ID obj in array) {16.  NSLog (@ "%i-%@", I, obj);  i++;  18.} 19.}    #pragma mark traverses the array 3 22.void ArrayFor3 () {23.  Student *STU1 = [Student Student];  Nsarray *array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil];     [Array enumerateobjectsusingblock:26.        ^ (ID obj, Nsuinteger idx, Boolbool *stop) {27.  NSLog (@ "%i-%@", idx, obj);         28.//If the index is 1, stop traversing 29.             if (idx = = 1) {30.             Use the pointer to modify the value of the outer bool variable 31. *stop = YES;    32.} 33.  }];  34.} 35.    #pragma mark traverses the array 4 37.void ArrayFor4 () {38.  Student *STU1 = [Student Student];  Nsarray *array = [Nsarray arraywithobjects:stu1, @ "1", @ "2", @ "3", nil Nil];    40.//Gets the array iterator 41.  Nsenumerator *enumerator = [array objectenumerator];    42.//reverse-order iterator (traversing element from tail) 43.  Nsenumerator *enumerator = [array reverseobjectenumerator];    The.//AllObjects is to remove objects that have not been traversed 45.  Nsarray *array2 = [enumerator allobjects];  NSLog (@ "array2:%@", array2);    47.//Gets the next element that needs to be traversed 48.  ID obj = nil;        (obj = [Enumerator nextobject]) {50.  NSLog (@ "obj=%@", obj);   51.} 52.}

Four, array sorting

Sort #pragma mark Array 1 02.void arraySort1 () {03.  Nsarray *array = [Nsarray arraywithobjects:@ "2", @ "3", @ "1", @ "4", nil Nil];    04.05.    Returns an ordered array that does not change the order of elements in the original array by 06.    The comparison method for the specified element: compare:07.  Nsarray *array2 = [Array sortedarrayusingselector: @selector (compare:)];  NSLog (@ "array2:%@", array2);  09.} 10.    #pragma mark Array sort 2 12.void arraySort2 () {13.  Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li"];  Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang"];  Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li"];  Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao"];  Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];    18.//Specify the sort comparison method 19.  Nsarray *array2 = [Array sortedarrayusingselector: @selector (comparestudent:)];  NSLog (@ "array2:%@", array2);  22.-} (Nscomparisonresult) Comparestudent: (Student *) Stu {23.//Sort by first name 24.  Nscomparisonresult result = [Self.lastname compare:stu.lastname];    25.//If you have the same surname, compare the name 26.        if (result = = Nsorderedsame) {27.  result = [Self.firstname compare:stu.firstname];    28.} 29.  return result;  30.} 31.    #pragma mark Array sort 3 33.void arraySort3 () {34.  Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li"];  Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang"];  Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li"];  Panax Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao"];  Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];    39.40.    Use block to sort 41.     Nsarray *array2 = [Array sortedarrayusingcomparator:42.         ^nscomparisonresult (Student *obj1, Student *obj2) {43.         Sort 44 by last name first.  Nscomparisonresult result = [Obj1.lastname compare:obj2.lastname]; 45.//If you have the same surname,Just compare the name 46.             if (result = = Nsorderedsame) {47.  result = [Obj1.firstname compare:obj2.firstname];           48.} 49.  . return result;  51.}];    52.53.  NSLog (@ "array2:%@", array2);  54.} 55.    #pragma mark Array Sort 4-advanced sort 57.void arraySort4 () {58.  Student *STU1 = [Student studentwithfirstname:@ "Mingjie" lastname:@ "Li" bookname:@ "Book1"];  Student *STU2 = [Student studentwithfirstname:@ "Longhu" lastname:@ "Huang" bookname:@ "Book2"];  Student *STU3 = [Student studentwithfirstname:@ "Lianjie" lastname:@ "Li" bookname:@ "Book2"];  Student *stu4 = [Student studentwithfirstname:@ "Jian" lastname:@ "Xiao" bookname:@ "Book1"];  Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil];    63.64.    1. Sort by title first 65.    Here the key is written by the name of @property 66.  Nssortdescriptor *booknamedesc = [Nssortdescriptor sortdescriptorwithkey:@ "Book.name" Ascending:YES];    67.//2. Sort by last name 68. Nssortdescriptor *lastnamedesc = [Nssortdescriptor sortdescriptorwithkey:@ "LastName" Ascending:yes];    69.//3. Sort by name 70.  Nssortdescriptor *firstnamedesc = [nssortdescriptor sortdescriptorwithkey:@ "FirstName" Ascending:yes];    71.//Add Sort descriptor 72 sequentially.  Nsarray *descs = [Nsarray arraywithobjects:booknamedesc, Lastnamedesc, Firstnamedesc, nil Nil];    73.74.  Nsarray *array2 = [array Sortedarrayusingdescriptors:descs];    75.76.  NSLog (@ "array2:%@", array2);   77.}

 

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.