iOS Development basics: A common approach to OC array Object Nsarray

Source: Internet
Author: User

This article describes the use of the basic method of an OC Array object: Because the array of OC is stored as an object type, we can create a new person class to manipulate with the person generation object. Where the code in Person.h is: [OBJC] View plain copy#import  @interfacePerson:nsobject {nsstring*_name;//nameNSString *_sex;//SexNsinteger _age;//Age}  -(Nsinteger) age; - (ID) Initwithname: (NSString *) name sex: (NSString *sex Age: (Nsinteger) age; - (void) Sayhi; -(Nscomparisonresult) Comparebyage: (Person *) per; //when%@ prints, the description method of the object is automatically called internally. //overriding the description method of the parent class-(NSString *) description; @endimplemented in PERSON.M: [OBJC] View plain copy#import "Person.h"  @implementation Person- (ID) Initwithname: (NSString *) name sex: (NSString *) Sex Age: (Nsinteger)=[Super Init]; if(Nil! =Self ) {_name=name; _sex=sex; _age=Age ; }      returnSelf ; }  -(Nsinteger) Age {return_age; }  - (void) Sayhi {NSLog (@"name:%@, sex:%@, Age:%ld", _name, _sex, _age); }  -(Nscomparisonresult) Comparebyage: (Person *) per {//Self Gets the current object, and per gets another object//Two objects Compare age    if([Self] <[per Age]) {          returnnsorderedascending;//-1}Else if([self] = =[per Age]) {          returnNsorderedsame;//0}Else {          returnnsordereddescending;//1    }  }  //when%@ prints, the description method of the object is automatically called internally. //overriding the description method of the parent class-(NSString *) Description {return[NSString stringWithFormat:@"%@,%@,%ld", _name, _sex, _age]; }  @endthe complete code in MAIN.M is: [OBJC] View plain copy#import  #import "Person.h"  intMainintargcConstCharchar *argv[]) {@autoreleasepool {//Create three person objects firstPerson *per1 = [[Person alloc] Initwithname:@"Zhangsan"Sex@"Mans"Age at]; person*per2 = [[Person alloc] Initwithname:@"Lisi"Sex@"woman"Age -]; person*per3 = [[Person alloc] Initwithname:@"Wangwu"Sex@"Mans"Age -]; person*Per4; //creates an array object to store three person objects. //for an array in OC, it is used to store multiple objects, and the type of the object can be arbitrary. //1.initWithObjects: Used to initialize the array to store multiple objects at once, after the colon filled in the array of objects to be stored, that is, the array elements, objects and objects separated by commas, and finally with nil as the end of the initialization of the array elements,          In other words, there are several objects before nil, and several objects are stored in the array, and the objects after nil are not stored in the array. //An array is an orderly combination, that is, the object that is put into the array is at the front of the array, and then placed in the back, in order. //and arrays are very similar to arrays in the C language, with the concept of subscript, each element has a corresponding subscript, the subscript range is starting from 0, to the number of array elements-1, and the subscript of the array cannot be crossed. Nsarray *per =[[Nsarray alloc] Initwithobjects:per1, Per2, Per3, Per2, nil Nil]; //2. How to obtain the number of array elementsNsuinteger count =[per Count]; NSLog (@"Count=%lu", Count); //3. Get objects based on index values//objectatindex: Gets the corresponding element of the subscript in the array according to the given subscript//Person *person = [per objectatindex:0]; //used to get the first element in the arrayPerson *person1 =[per Firstobject]; //[Person Sayhi]; [Person1 Sayhi]; //gets the last elementPerson *person2 =[per Lastobject];                    [Person2 Sayhi]; person*person3 = [per objectatindex:[per count]-1];                    [Person3 Sayhi]; //4. Get the index value for the object//The first subscript corresponding to the object is always obtained from the specified object. //If the specified object is not in the array, an infinite number is returned Long_maxNsuinteger index =[per Indexofobject:per4]; NSLog (@"index =%lu", index); //5. Determine if two arrays are the same. Nsarray *newarr =[Nsarray arraywithobjects:per1, Per2, Per3, nil Nil]; //determine if Newarr and per are the same//Isequaltoarray: Determines whether the contents of two arrays are equal (that is, whether the elements within the array correspond to equal)BOOL isequal =[per Isequaltoarray:newarr]; NSLog (@"%d", isequal); //(1). An array is an ordered set of elements that are added first in the front of the array. //(2). The elements in the array can be duplicated. NSLog (@"count=%ld", [per Count]); //used to determine whether two objects are equal (that is, two are the same object)//for collections, if you use the isequal: or Isequalto: method, the internal implementation is also the comparison of the elements within the collection that corresponds to equality. BOOL IsEqual1 =[per Isequalto:newarr]; NSLog (@"isequal =%d", IsEqual1); Nsset*set1 =[Nsset Setwitharray:per]; Nsset*set2 =[Nsset Setwitharray:newarr]; BOOL is=[Set1 Isequalto:set2]; NSLog (@"Is --->%d", is); //6. Array sorting, ascending orderNsarray *strarr = [Nsarray arraywithobjects:@"BB",@"cc",@"AA",@"DD",@"FF",@"BB", nil Nil]; NSLog (@"%@", Strarr); //Selector Method Selector//assuming that each object in the array is a string object, we use the string's own comparison method compare://Nsarray *sortarray =[Strarr sortedarrayusingselector: @selector (compare:)]; NSLog (@"%@", Sortarray); //creates a new array with objects of four person types, and then arranges the array in ascending order by the age of the personPerson *pers1 = [[Person alloc] Initwithname:@"Zhangsan"Sex@"Mans"Age at]; person*pers2 = [[Person alloc] Initwithname:@"Lisi"Sex@"woman"Age -]; person*PERS3 = [[Person alloc] Initwithname:@"Wangwu"Sex@"Mans"Age -]; person*PERS4 = [[Person alloc] Initwithname:@"Zhangsan"Sex@"Mans"Age -]; person*PERS5 = [[Person alloc] Initwithname:@"Lisi"Sex@"woman"Age -]; person*PERS6 = [[Person alloc] Initwithname:@"Wangwu"Sex@"Mans"Age +]; Nsarray*perarr =[Nsarray arraywithobjects:pers1, Pers2, Pers3, PERS4, PERS5, PERS6, nil Nil]; Nsarray*sortarr =[Perarr sortedarrayusingselector: @selector (comparebyage:)]; NSLog (@"%@", Sortarr); //7. Logarithmic group Traversal         for(inti =0; i < [Perarr count]; i++) {              //get array elements based on subscriptPerson *per =[Perarr objectatindex:i]; //output per's information//NSLog (@ "%@", [per description]); [per Sayhi]; }                    //fast traversal of arrays//Remove the element one by one from the collection in turn         for(Person *pinchPerarr)          {[P sayhi]; }                }      return 0; You can copy the code to the project, or you can read the code's comments directly. Where the contents of the printed array in the main function are used in a variety of output modes, you can learn the output of the array. The comparison selector itself defines a method used to declare the method of comparison. 

iOS Development basics: A common approach to OC array Object 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.