NSArray arrays are commonly handled in iOS

  • 2020-10-23 20:18:14
  • OfStack

1. Common processing methods of arrays


//-------------------- Immutable array 
//1. Array creation 
NSString *s1 = @"zhangsan";
NSString *s2 = @"lisi";
NSString *s3 = @"wangwu";
//(1)
NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil];
NSLog(@"%@",array1); // Is equivalent to  array1.descripton
//(2) Create using class methods 
NSArray *array2 = [NSArray arrayWithObjects:s1,s2,s3, nil];
//(3) create 1 I'm going to store an array of objects 1 The element 
NSArray *array3 = [NSArray arrayWithObject:s1];
//(4) create 1 An array whose elements are derived from array1
NSArray *array4 = [NSArray arrayWithArray:array1];
NSLog(@"array4 = %@",array4);
//2. You take the element by subscripting it 
NSString *str1 = [array4 objectAtIndex:0];
//3. Number of array elements 
NSUInteger count = [array4 count]; // Is equivalent to: array4.count;
//4. Determines whether an array contains an element 
BOOL isContains = [array4 containsObject:@"zhangsan"];
NSLog(@"isContains:%d",isContains);
//5. To find a 1 The index position of an object in an array 
NSUInteger index = [array4 indexOfObject:@"wangwu"];
if (index == NSNotFound) {
NSLog(@"Not find elemnts");
} else {
NSLog(@"index = %ld",index);
}
//6. String in the link array ( Premise: arrays are full of strings. 
NSString *joinString = [array4 componentsJoinedByString:@","];
NSLog(@"joinString = %@",joinString);
//7. Accessing the end of the array 1 An element 
NSString *lastObj = [array4 lastObject]; //array4.lastObject
NSLog(@"lsatObj = %@",lastObj);
//8. Add an array of reroute paths 1 An element 
NSArray *array5 = [array4 arrayByAddingObject:@"zhaolia"];
NSLog(@"array5 = %@",array5);
// Take the subscript element 
int idx=4;
if (idx <array5.count) {
NSString *s = [array5 objectAtIndex:idx];
NSLog(@"s = %@",s);
}
//-------------- Traversal of an array 
//1. Ordinary traversal 
for (int i=0; i<array5.count; i++) {
NSString *str = [array5 objectAtIndex:i];
NSLog(@"%@",str);
}
// Rapid traverse 
for (NSString *s in array5) {
NSLog(@"%@",s);
}
//4.4 Subsequent optimization 
//1. create 1 A group 
NSArray *array7 = @[s1,s2,s3]; // Is equivalent to: NSArray *array7 = [NSArray arrayWithObjects:s1,s2,s3,nil];
NSLog(@"array7 = %@",array7);
NSString *str = array7[0];
NSLog(@"array[7] = %@",str);
//------------------ An array variable 
// Initialize, set the number of elements as 5 Can be changed. (inherited from NSArray ) 
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:5];
// Want to add to the array 1 An element 
[mutableArray addObject:@"aaa"];
// Inserts elements into an array with a specified index 
[mutableArray insertObject:@"ccc" atIndex:0];
NSLog(@"%@",mutableArray); // The original position element is moved back 
// Remove the last 1 An element 
[mutableArray removeLastObject];
NSLog(@" After removing the last element: %@",mutableArray);
// Removes the specified element 
[mutableArray removeObject:@"aaa"];
// Removes the specified subscript element 
[mutableArray removeObjectAtIndex:0];
// Add an array to an array 
[mutableArray addObjectsFromArray:array1];
//1. Create a mutable array 
NSString *t1 = @"zhangsan ";
NSString *t2 = @"lisi";
NSString *t3 = @"wangwu ";
// NSMutableArray *mArray1 = @[s1,s2,s3];//wrong. So here we have created 1 An immutable array 
NSMutableArray *mArray1 = [[NSMutableArray alloc] initWithObjects:s1,s2,s3, nil];
// When you create an array, create 3 Space is used to store elements. When storage exceeds capacity, the array automatically increases space 
NSMutableArray *mArray2 = [[NSMutableArray alloc] initWithCapacity:3];
NSMutableArray *mArray3 = [NSMutableArray arrayWithCapacity:3];
//2. Add elements 
[mArray2 addObject:t1];
[mArray2 addObject:t2];
[mArray2 addObject:t3];
NSLog(@"mArray2= %@",mArray2);
// will mArray2  Element added to mArray3 In the 
// [mArray3 addObjectsFromArray:mArray2];
// will mArray2 As a 2 Dimensional digital addition 
[mArray3 addObject:mArray2];
NSLog(@"mArray3 = %@",mArray3);
//3. Insert elements 
[mArray2 insertObject:@"Jack" atIndex:0];
NSLog(@"mArray2 = %@",mArray2);
//4. Replace the element 
[mArray2 replaceObjectAtIndex:0 withObject:@"John"];
NSLog(@" Replacement: %@",mArray2);
//5. Swap the positions of the two elements 
[mArray2 exchangeObjectAtIndex:3 withObjectAtIndex:0];
NSLog(@"mArray2 = %@",mArray2);
//6. Remove elements 
//6.1 Delete according to subscript 
[mArray2 removeObjectAtIndex:2];
NSLog(@"mArray2 = %@",mArray2);
//6.2 Delete the last 1 An element 
[mArray2 removeLastObject];
NSLog(@"mArray2 = %@",mArray2);
//6.3 Deletes the specified object 
//[mArray2 removeObject:@"zhangsan"];
//6.4 Delete all elements 
[mArray2 removeAllObjects];
NSLog(@"mArray2 = %@",mArray2);

Related articles: