Nsarray Basic Usage

Source: Internet
Author: User

Nsarray Array

class method to create an array (empty array, no meaning)
Nsarray *array = [Nsarray array];

An instance method creates an array (an empty array)
Nsarray *array1 = [[Nsarray alloc]init];

Create an array and initialize the element nil to indicate the end of the array
Nsarray *array2 = [Nsarray arraywithobjects:@ "Wukong", @ "eight commandments", @ "baigujing", nil];

Use an array to create a new array (equivalent to an array copy)
Nsarray *array3 = [Nsarray arraywitharray:array2];


Quickly create
Nsarray *array4 = @[@ "1", @ "ret", @ "FRSGDFH"];

Instance method specifies the object creation array
Nsarray *array5 = [[Nsarray alloc]initwithobjects:@ "3", @ "JDS", @ "F", Nil];

Number of array elements
NSLog (@ "%ld", [Array5 Count]);

Accessing array elements
NSLog (@ "%@", [Array5 objectatindex:1]);
NSLog (@ "%@", array5[1]);

Whether to include an object (the IsEqual method that invokes the object)
BOOL iscontain = [Array containsobject:@ "1"];

String Cut Array
NSString *string7 = @ "20|http://www.baidu.com";
Nsarray *a = [String7 componentsseparatedbystring:@ "|"];
for (int i = 0; i < [a count]; i++) {
NSLog (@ "%@", A[i]);
}



Variable groups (be sure to initialize)
Create a mutable array (cannot be created as quickly as Nsarray)
Nsmutablearray *marray = [Nsmutablearray array];
Nsmutablearray *mutablearray1 = [[nsmutablearray alloc]init];//instance variable initialization
The array can hold the number of elements when created
Nsmutablearray *arr = [Nsmutablearray arraywithcapacity:10];



Adding an array element
[MutableArray1 addobject:@ "Xiaobai"];
adding multiple array elements
Mutablearray1.array = @[@ "ds", @ "3", @ "RT"];
Inserts an element in the specified subscript
[MutableArray1 insertobject:@ "Xiaohei" atindex:1];

Delete element at end of array
[MutableArray1 Removelastobject];

Delete all elements
[MutableArray1 removeallobjects];

Delete array corresponding elements based on element object (compare by IsEqual method)
[MutableArray1 removeobject:@ "Xiaohei"];

Delete the corresponding element according to the array element subscript
[MutableArray1 removeobjectatindex:0];

Iterating through an array
Nsarray *array6 = [Nsarray arraywithobjects:@ "Naruto", @ "Luffy", @ "Sasuke", @ "Zoro", @ "Conan", @ "Kakashi", nil];
for (int i = 0; i < [Array6 count]; i++) {
NSLog (@ "%@", [Array6 objectatindex:i]);
NSLog (@ "%@", Array6[i]);
}

For (NSString *str in Array6) {
NSLog (@ "%@", str);
}

Gets the first element of the array
NSLog (@ "%@", [Array6 Firstobject]);

Gets the last element of the array
NSLog (@ "%@", [Array6 Lastobject]);



Array of string turns
NSString *string = @ "1 2 3 4 5 6";
Use a string to split the original string into an array
Nsarray *ARRAY14 = [string componentsseparatedbystring:@ ""];

NSLog (@ "%@", ARRAY14);
Assemble the array string
NSString *str = [Array componentsjoinedbystring:@ "-"];
NSLog (@ "%@", str);



Let all elements in the array execute a method
[Array makeobjectsperformselector: @selector (bark)];
[Array makeobjectsperformselector: @selector (Eat:) withobject:@ "Bone"];

Iterating through an array
Use the normal for
Nsarray *ARR3 = @[@ "Re", @ "FD", @ "S"];

for (int index = 0; index < array.count; index++) {
NSString *sss = Array[index];
NSLog (@ "%@", SSS);
}
Use Add for (most commonly used)

For (NSString *str in arr) {

NSLog (@ "%@", str);

}

Through the enumerator
Nsenumerator *enumer = [arr objectenumerator];
NSString *newdog;
while (Newdog = [Enumer Nextobject]) {
NSLog (@ "%@", Newdog);
}

by block mode
The IDX represents this element in the array index
Stop is set to Yes so it doesn't traverse backwards.
[Arr enumerateobjectsusingblock:^ (NSString * dog, Nsuinteger idx, BOOL *stop) {
NSLog (@ "%@-%zd", DOG,IDX);
if (idx = = 2) {
*stop = YES;
}
}];


Array sorting
Using the comparator (block)
Array = [Array Sortedarrayusingcomparator:^nscomparisonresult (Czdog * obj1, Czdog * obj2) {
//
if (obj1.age = = obj2.age) {
return [Obj1.name Compare:obj2.name];
}else if (Obj1.age > Obj2.age) {
return nsordereddescending;//Descending
}else{
return nsorderedascending;//Ascending
//            }
//
return [Obj1.name Compare:obj2.name];
//        }];
NSLog (@ "%@", array);
Property Descriptor Comparison
Nssortdescriptor *DESC1 = [nssortdescriptor sortdescriptorwithkey:@ "age" ascending:yes];
Nssortdescriptor *DESC2 = [nssortdescriptor sortdescriptorwithkey:@ "name" Ascending:yes];

Array = [array sortedarrayusingdescriptors:@[desc1,desc2]];
NSLog (@ "%@", array);

Nsarray Basic Usage

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.