SoFunction
Updated on 2025-04-12

Common processing methods for NSArray arrays in iOS

1. Common processing methods for arrays

//-----------------------------------------------------------------------------------------------------------------------------//1.Array creationNSString *s1 = @"zhangsan";
NSString *s2 = @"lisi";
NSString *s3 = @"wangwu";
//(1)
NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil];
NSLog(@"%@",array1); //Equivalent to//(2) Create using class methodNSArray *array2 = [NSArray arrayWithObjects:s1,s2,s3, nil];
//(3) Create an array object and store an element into it at the same time.NSArray *array3 = [NSArray arrayWithObject:s1];
//(4) Create an array, the elements in this array are from array1NSArray *array4 = [NSArray arrayWithArray:array1];
NSLog(@"array4 = %@",array4);
//2. Take elements by subscriptNSString *str1 = [array4 objectAtIndex:0];
//3. Number of array elementsNSUInteger count = [array4 count]; //Equivalent to:;//4. Determine whether the array contains an elementBOOL isContains = [array4 containsObject:@"zhangsan"];
NSLog(@"isContains:%d",isContains);
//5. Find the subscript position of an object in the arrayNSUInteger index = [array4 indexOfObject:@"wangwu"];
if (index == NSNotFound) {
NSLog(@"Not find elemnts");
} else {
NSLog(@"index = %ld",index);
}
//6. Link strings in arrays (prerequisite: all strings in arrays)NSString *joinString = [array4 componentsJoinedByString:@","];
NSLog(@"joinString = %@",joinString);
//7. Access the last element of the arrayNSString *lastObj = [array4 lastObject]; //
NSLog(@"lsatObj = %@",lastObj);
//8. Add an element after the original arrayNSArray *array5 = [array4 arrayByAddingObject:@"zhaolia"];
NSLog(@"array5 = %@",array5);
//Please the corresponding subscript elementint idx=4;
if (idx <) {
NSString *s = [array5 objectAtIndex:idx];
NSLog(@"s = %@",s);
}
//-----------------------------//1. Ordinary traversalfor (int i=0; i<; i++) {
NSString *str = [array5 objectAtIndex:i];
NSLog(@"%@",str);
}
//Quick traversalfor (NSString *s in array5) {
NSLog(@"%@",s);
}
// Optimization after 4.4//1. Create a material groupNSArray *array7 = @[s1,s2,s3]; //Equivalent to: NSArray *array7 = [NSArray arrayWithObjects:s1,s2,s3,nil];NSLog(@"array7 = %@",array7);
NSString *str = array7[0];
NSLog(@"array[7] = %@",str);
//-----------------------------------------------------------------------------------------------------------------------------//Initialize, set the number of elements to 5, which can be changed.  (Inherited from NSArray)NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:5];
//I want to add an element to the array[mutableArray addObject:@"aaa"];
//Insert element to specify the subscript to the array[mutableArray insertObject:@"ccc" atIndex:0];
NSLog(@"%@",mutableArray); //The original position element moves back//Remove the last element[mutableArray removeLastObject];
NSLog(@"After removing the last element: %@",mutableArray);
//Remove the specified element[mutableArray removeObject:@"aaa"];
//Remove the specified subscript element[mutableArray removeObjectAtIndex:0];
//Add an array to the array[mutableArray addObjectsFromArray:array1];
//1. Create a variable arrayNSString *t1 = @"zhangsan ";
NSString *t2 = @"lisi";
NSString *t3 = @"wangwu ";
// NSMutableArray *mArray1 = @[s1,s2,s3];//wrong. An immutable array is created hereNSMutableArray *mArray1 = [[NSMutableArray alloc] initWithObjects:s1,s2,s3, nil];
//When creating an array, open up 3 spaces for storing elements. If the storage exceeds the capacity, the array will automatically increase the 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);
//Add elements in mArray2 to mArray3// [mArray3 addObjectsFromArray:mArray2];
//Add mArray2 as a 2D number[mArray3 addObject:mArray2];
NSLog(@"mArray3 = %@",mArray3);
//3. Insert element[mArray2 insertObject:@"Jack" atIndex:0];
NSLog(@"mArray2 = %@",mArray2);
//4. Replace elements[mArray2 replaceObjectAtIndex:0 withObject:@"John"];
NSLog(@"replace:%@",mArray2);
//5. Swap the positions of two elements[mArray2 exchangeObjectAtIndex:3 withObjectAtIndex:0];
NSLog(@"mArray2 = %@",mArray2);
//6. Delete elements//6.1 Delete according to subscript[mArray2 removeObjectAtIndex:2];
NSLog(@"mArray2 = %@",mArray2);
//6.2 Delete the last element[mArray2 removeLastObject];
NSLog(@"mArray2 = %@",mArray2);
//6.3 Delete the specified object//[mArray2 removeObject:@"zhangsan"];
//6.4 Delete all elements[mArray2 removeAllObjects];
NSLog(@"mArray2 = %@",mArray2);

The above is the commonly used processing method of NSArray arrays in iOS introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!