IOS stage learning 15th-day notes (NSArray and NSMutableArray arrays), iosnsarray sorting

Source: Internet
Author: User

IOS stage learning 15th-day notes (NSArray and NSMutableArray arrays), iosnsarray sorting

Knowledge points of IOS Learning (oc language)

1. array in OC

1) array: it is also an object. The address of the object is stored in the array, and the address of any type of object can be stored. The array can only be an object rather than a specific value,
Repeated elements can be stored. arrays are also divided into immutable arrays and variable arrays.
1. unchangeable array NSArray: the array content cannot be modified after Initialization
2. variable array NSMutableArray: you can modify the content of an array at any time (adding, deleting, and modifying elements)

2. NSArray Array Operations

1) Create an NSArray Array

1. Method 1: Use the instance method to create an array object, for example:

1 NSArray * array1 = [[NSArray alloc] initWithObjects: @ "one", 2 [NSNumber numberWithInt: 20], @ "three", @ "one", @ "four ", nil]; 3 // use an array object to create another array object 4 NSArray * array2 = [[NSArray alloc] initWithArray: array1];


2. Create an array object using the class method, for example:

1 NSArray *array3=[NSArray arrayWithObjects:@"one",@"two",@"three",@"four", nil];

 

3. Method 3: directly create an array. For example:

1 NSArray *array4=@[@"one",@"two",@"three",@"four"];


2) You can use. count to obtain the number of elements in the array, for example, 1 NSUInteger number = array1.count;


3) objectAtIndex is used to obtain the elements in the Array Based on the array subscript, for example:

1 NSString * str1 = [array1 objectAtIndex: 0]; // obtain the first element in the array

2 NSString * str2 = array1 [0]; // retrieve the element corresponding to the subscript using a method similar to the C Language


4) firstObject is used to obtain the first element in the array, for example:
1 NSString * first = [array1 firstObject];

5) lastObject is used to obtain the last element in the array, for example:
1 NSString * last = [array1 lastObject];

6) indexOfObject is used to obtain the subscript of the element in the Array Based on the element. For example:
1 NSUInteger index = [array1 indexOfObject: @ "three"]; // The result is: 2

7) containsObject is used to determine whether an element exists in the array, for example:
 

1 if([array1 containsObject:@"four"]){2            NSLog(@"contains");3 }

 

8) NSArray array Traversal method

1. Loop traversal: obtain each element by subscript, for example:

1  for(int i=0;i<array1.count;i++){2         NSString *s=[array1 objectAtIndex:i];3         NSLog(@"%@",s);4   }


2. iterator method: Create an iterator object and traverse the elements of array1, for example:

1 NSEnumerator * enumerator = [array1 objectEnumerator]; 2 id obj; 3 // [enumerator nextObject]: The iterator automatically points to the next element. If no element exists, is nil4 while (obj = [enumerator nextObject]) {5 NSLog (@ "% @", obj); 6}


3. Fast traversal: The content of elements that cannot be modified over time is as follows:

1 // in the loop, each element in the array is automatically assigned to s2 for (id s in array1) {3 if ([s isKindOfClass: [NSNumber class]) // retrieve the numeric data in the array 4 NSLog (@ "number ******** % @", s); 5 else if ([s isKindOfClass: [NSString class]) {// retrieves data of the string type in the array 6 NSLog (@ "nsstring ******* % @", s); 7} 8}


9) directly printing an array will automatically call the description method of the elements in the array and display all the elements. For example:
1 NSLog (@ "% @", array1); // Note: Chinese content becomes garbled and needs to be traversed cyclically.

10) componentsJoinedByString is used to concatenate each element in the array with a specified character into a string, for example:
1 NSString * s = [array1 componentsJoinedByString: @ ","]; // concatenate each element with a comma

11) componentsSeparatedByString is used to split a string into an array with specified characters. For example:

1 NSArray * array6 = [str componentsSeparatedByString: @ ", @"]; // use commas to separate string 2

// Note: Split the string into sub-strings based on any character in the character set object. @ "" may occur and you need to manually process it.

12) componentsSeparatedByCharactersInSet creates a character set object with all the characters in the string, for example:

1 NSString *str=@"hello,@nihao!shang,hai";2 NSArray *array7=[str componentsSeparatedByCharactersInSet:3 [NSCharacterSet characterSetWithCharactersInString:@",@!"]];


3. NSMutableArray Array Operations

1) variable array: the parent class is NSArray. NSMutableArray can operate on elements in the array (add, delete, and modify)

2) initWithObjects is used to create a variable array object, for example:
1 NSMutableArray * array1 = [[NSMutableArray alloc] initWithObjects: @ "one", @ "two", nil];

3) addObject adds an object at the end of the array, for example:
1 [array1 addObject: [NSNumber numberWithInt: 100];

4) insertObject... AtIndex... Insert an object at the specified index position of the array, for example:
1 [array1 insertObject: @ "shanghai" atIndex: 0];

5) removeObject deletes a specified element. If multiple elements exist, all elements are deleted. For example:
1 [array1 removeObject: @ "one"];

6) removeLastObject is used to delete the last element, for example:
1 [array1 removeLastObject];

7) removeObjectAtIndex is used to delete elements at the specified position in the array, for example:
1 [array1 removeObjectAtIndex: 2];

8) removeObject... InRange... Delete the elements in the specified range (if not specified, all identical elements will be deleted). For example:
1 [array1 removeObject: @ "two" inRange: NSMakeRange (0, 3)];

9) removeAllObjects:
1 [array1 removeAllObjects];

10) replaceObjectAtIndex... WithObject... Replace the element at the specified position with a new element, for example:
1 [array1 replaceObjectAtIndex: 0 withObject: @ "hello"];

11) exchangeObjectAtIndex... WithObjectAtIndex... Used to exchange elements at a specified subscript, for example:
1 [array1 exchangeObjectAtIndex: 0 withObjectAtIndex: 2];

 

12) setArray resets the content in the array, for example:

1 NSArray *subArray=@[@"ios",@"two",@"oc"];2 [array1 setArray:subArray];

 

13) NSMutableArray adds a sorting operation for class objects.
Instance code:

// NSMutableArray * array = [[NSMutableArray alloc] init]; Student * stu = [[Student alloc] init]; stu. name = @ "KingKong"; stu. age = 24; stu. no = @ "A1088"; stu. score = 99; [array addObject: stu]; // NSSortDescriptor * sortDes = [NSSortDescriptor sortscriptorwithkey: @ "score" ascending: YES] [array sortUsingDescriptors: [NSMutableArray arrayWithObject: sortDes];

 

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.