Objective-c Nsarray method

Source: Internet
Author: User

Nsarray (Array)

Create three string objects

NSString *str1 = [NSString stringwithformat:@ "IPhone4"];

NSString *STR2 = [[NSString alloc] initwithformat:@ "IPhone5"];

NSString *STR3 = @ "IPhone6";

NSLog (@ "%@%@%@", STR1,STR2,STR3);

Nsarray

method 1:initwithobjects because it is an array, you need to pass in multiple objects, which are separated by "," and end with nil.

Create an Array object to receive the incoming objects .

Nsarray *arr1 = [[Nsarray alloc] initwithobjects:str1,str2,str3, nil];

NSLog (@ "%@", arr1);

Method 2:objectatindex: Finding an object by subscript will only find the first conforming object , even after the first object is not displayed , after the first one is found, it returns back .

NSString *str = [arr1 objectatindex:1];

NSLog (@ "%@", str);//nsstring type IPhone5

Nsinteger index = [arr1 INDEXOFOBJECT:STR2];

NSLog (@ "%ld", index),//arr1 subscript 1

Method 3: View the number of array elements

Nsinteger count = [arr1 count];

NSLog (@ "%ld", count);// Result: There are 3 elements in arr1

Method 4: Print out individual elements by facilitating

for (int i = 0; i < Arr1.count; i++) {

NSLog (@ "%@", [arr1 objectatindex:i]);

}

Method 5: sort sortedarrayusingselector: @selector (compare:) This method is provided by the system, the interior has been sorted, so know the method is good, There is no need for undue investigation.

Nsarray *sortarray = [arr1 sortedarrayusingselector: @selector (compare:)];

NSLog (@ "%@", Sortarray);

Nsmutablearray (variable group)

Nsmutablearray inheritance and Nsarray so Nsarray method Nsmutablearray can also be used

creating a mutable Array object

Nsmutablearray *mutarray = [[Nsmutablearray alloc] initwithobjects:str1,str3, nil];

Method 1: add addobject

[Mutarray ADDOBJECT:STR1];

[Mutarray ADDOBJECT:STR2];

[Mutarray ADDOBJECT:STR3];

NSLog (@ "%@%@%@", STR1,STR2,STR3);

Method 2: Delete the Removeobjectatindex

[Mutarray removeobjectatindex:1];

[Mutarray removeobjectatindex:0];

[Mutarray Removeobjectatindex:2];

NSLog (@ "%@", Mutarray);

Method 3: swap exchangeobjectatindex:

[Mutarray exchangeobjectatindex:1 withobjectatindex:0];

NSLog (@ "%@", Mutarray);

Method 4: sort sortusingselector: @selector (compare:)

nsstring *str1 = @ "Jack";

nsstring *str2 = @ "Henry";

nsstring *STR3 = @ "Elyse";

nsstring *STR4 = @ "John";

nsstring *STR5 = @ "Justin";

Nsmutablearray *mutablearray = [[nsmutablearray alloc] INITWITHOBJECTS:STR1,STR2,STR3,STR4,STR5, Nil];

[Mutablearray Sortusingselector:@selector (compare:)];

NSLog (@ "%@", Mutablearray);

Method 5: Get the first element of the array firstobject

[Mutablearray firstobject];

NSLog (@ "%@", Mutablearray. Firstobject);

Method 5: get the last element of the array

[Mutablearray lastobject];

NSLog (@ "%@", Mutablearray. Lastobject);

}

return 0;

Objective-c Nsarray method

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.