The use of Ios--oc--nsarray

Source: Internet
Author: User

NsarrayEssentially, Nsarray is a container for storing objects. Nsarray can store only objective-c objects, not basic data types in C, such as: Int,flot, pointers, and so on. When using Nsarray, pay special attention to the need to end with nil. To represent the end. Nsarray creates a static array, and once created, it is no longer possible to add and delete objects in the array.   creation of 1.NSArray:class method to create an array:
    1. Nsarray *array1 = [Nsarray arraywithobject:@ "obj"];
    2. Nsarray *array2 = [Nsarray arraywithobjects:@ "Obj1", @ "Obj2", @ "Obj3", nil];
    3. Nsarray *array3 = [Nsarray arraywitharray:array2];
instance method to create an array:Nsarray *array4 = [[Nsarray alloc] initwithobjects:@ "AAA", @ "BBB", nil]; Nsarray *arr = [Nsarray array]; //Initialize an empty array (not commonly used); Nsarray *arr1 = [Nsarray arraywithobject:@ "123"];NSLog (@ "%@", arr1); //Initialize an array containing an element; Nsarray *arr2 = [Nsarray arraywithobjects:@ "Zhangyu", @ "Zhangdawei", @ "Liuhuan", nil]; //Initializes an array of multiple elements, separated by commas, and ends with nil; Nsarray *arr3 = @[@ "123",@ "456",@ "789"                          ]; //Initialize multiple elements with a comma between the elements of the array, note that the last element is not followed by a comma (if the element is a class object without double quotes).  use of 2.NSArray:(1) to find the length of the array:Nsuinteger count = Arr3.count; the number of elements in the array (the length of the array); NSLog (@ "Number of array elements:%zi", count);NSLog (@ "Number of array elements:%zi", arr3.count); array = = nil and [array count] = = 0The former is true to indicate that the object is nil, is an empty pointer, and it is not even created. The latter is true to indicate that the object is nil or has no elements.  (2) Take the elements in the array[Arr3 Firstobject]; //The first element of an array
[Arr3 Lastobject]; //The last element of the array
[Arr3 Objectatindex:2]; //array element at the specified location
long int i = [ARR3 indexofobject:@ "123"]; //Gets the position of an element in the array (the data type is Long/long int/nsuinteger (integer integer Integral whole thing)); NSLog (@ "i=%ld", I);(3) determine if an element is contained in an arrayBOOL flag = [ARR3 containsobject:@ "123"]; //Determine if an array contains an element (1 contains, 0 does not contain) NSLog (@ "flag=%d", flag); (4) array traversal (very important)<1>for Loop Traversal (most commonly used)for (int i = 0;i < arr3.count;i++)
        {
nsstring *str = arr3[i];
NSLog (@ "%@", str);
//id obj = [Arr3 objectatindex:i];
//nslog (@ "%@", obj);        }<2> iterating with enumeratorsnsenumerator *e = [Arr3 objectenumerator]; //Get an enumerator over all elements of the array
//[e Nextobject] First traversal, Nextobject points to the first element of the array, and when the value is finished, point to the next element until the last element is taken, at which point the nextobject points to nil, the value is 0, and the traversal ends
ID obj;
While (Obj=[e Nextobject])NSLog (@ "%@", obj);<3> Fast Enumeration (fastest, but no subscript required) (no nsenumerator *e = [Arr3 objectenumerator]; traverse directly with for)For (ID obj1 in ARR3)NSLog (@ "%@", obj1);<4> Array holds the object of the custom class (defines a student class and Name,number property, initializes 5 objects)Student *stu1 = [[Student alloc]init];
stu1.name = @ "Zhang Yi";
stu1.number = 201501;
Student *STU2 = [[Student alloc]init];
stu2.name = @ "Zhang Yi";
stu2.number = 201502;
Student *stu3 = [[Student alloc]init];
stu3.name = @ "Zhang San";
stu3.number = 201503;
Student *stu4 = [[Student alloc]init];
stu4.name = @ "Zhang Si";
stu4.number = 201504;
Student *stu5 = [[Student alloc]init];
stu5.name = @ "Zhang Five";
stu5.number = 201505;
nsarray *studentarr2 [email protected][stu1,
STU2,
Stu3,
Stu4,
Stu5
                                ];
for (int i = 0; i < Studentarr2.count; i++) {id stu = studentarr2[i]; //Determine if an object is a class and is used to handle type uncertainties
if ([Stu Iskindofclass:[student class]]) {
NSLog (@ "Name:%@:%d", [stu Name],[stu number]);
                          }                 }results of the operation:Name: Zhang Yi School Number: 201501
Name: Zhang Yi School Number: 201502
Name: Zhang San School Number: 201503
Name: Zhang Xi No.:201504
Name: Steven Cheung No.:201505 (5) sorting (from small to large)Nsarray *ARR4 = @[@ "6", @ "9", @ "8", @ "1"];Nsarray *ARR5 = [ARR4 sortedarrayusingselector: @selector (compare:)]; (the element used in the array is not an object (it is a string case!) ))NSLog (@ "%@", ARR5); Join first create a student class, initialize four objects Stu1,stu2,stu3,stu4 put into the array, sort by requirementssorting method Definition:-(Nscomparisonresult) Comparewithscore: (Student *) Stu;sorting method implementation:-(Nscomparisonresult) Comparewithscore: (Student *) Stu
{
Nscomparisonresult result;
if (Self.score < Stu.score)
result = 1;
Else result =-1;
return result;}Main program:Nsarray *studentarr = @[stu1,STU2,
Stu3,Stu4 //array element is the sort of object of a class ]; //The first method:
Nsarray *scorearr1 = [Studentarr sortedarrayusingselector: @selector (comparewithscore:)];
NSLog (@ "scorearr1=%@", scorearr1);
///second method:
Nsarray *scorearr2 = [Studentarr sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {
Nscomparisonresult result =-1;
           
if ([Obj1 score] < [OBJ2 score])
result = 1;
return result;
           
        }];
NSLog (@ "scorearr2=%@", SCOREARR2);
//Third Method (Description method):
nssortdescriptor *sort = [nssortdescriptor sortdescriptorwithkey:@ "score" ascending:no];
Nsarray *desarr = @[sort];
Nsarray *SCOREARR3 = [Studentarr Sortedarrayusingdescriptors:desarr];NSLog (@ "scorearr3=%@", SCOREARR3); //According to the student's grades from low to high order, if the same score, then the age from small to large sort * (Description method) nssortdescriptor *sort1 = [nssortdescriptor sortdescriptorwithkey:@ "score" ascending:yes];
nssortdescriptor *sort2 = [nssortdescriptor sortdescriptorwithkey:@ "age" ascending:yes];

Nsarray *desarr1 = @[sort1,sort2];
Nsarray *scoreagearr = [Studentarr sortedarrayusingdescriptors:desarr1];NSLog (@ "scoreagearr=%@", Scoreagearr); 

The use of Ios--oc--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.