Objective, object
The two most common containers for storing data in OC are arrays and dictionaries. As the most commonly used containers, we should understand all these features and usage.
The array in OC is a capacity that manages a series of elements in sequence and stores the elements in the array. It must be of the object type.
Immutable array, known and unchangeable. Once created successfully, the array capacity and elements cannot be changed.
Create an immutable array object
// 1. Initialization Method (multiple objects are separated by commas, and there must be a comma between the last object and nil)
NSArray * array1 = [[NSArray alloc] initWithObjects: @ "zhonger", @ "honghuang", @ "taixu", @ "zhili", nil]; // nil is not an object, it is just a flag that tells the compiler that the array assignment is complete.
// 2. Constructor
NSArray * array2 = [NSArray arrayWithObjects: @ "zhuba", @ "bada", @ "zhonger", @ "duliu", nil];
The printed data is displayed as (), for example:
array1 is ( zhonger, honghuang, taixu, zhili)
// 3. Laugh at the syntax sugar, literally this is the writing of an unchangeable array...
// Arrays in OC can store different types of objects
NSArray * array3 = @ [@ "yousiyi", @ "huangshenme", @ "shuishen", @ "qiuxiang", @ 12];
// Two core methods of array count objectAtIndex:
// Count calculates the number of array elements
Unsigned long count = [array1 count];
NSLog (@ "array1count is % lu", count); // you can print the number of elements.
// ObjectAtIndex: Find the corresponding element through the given subscript
NSString * bada = [array2 objectAtIndex: 1];
NSLog (@ "bada is % @", bada );
NSString * zhonger = array2 [2]; // syntactic sugar expression
NSLog (@ "zhonger is % @", zhonger );
// Traverse every element in array3
Unsigned long count1 = [array3 count];
For (int I = 0; I <count1; I ++ ){
NSLog (@ "% @", array3 [I]); // syntactic sugar
}
// Determine whether the array contains an element
BOOL isTrue = [array2 containsObject: @ "zhuba"];
NSLog (@ "isTrue is % d", isTrue );
// Obtain the subscript of the array where an element is located
NSUInteger index = [array2 indexOfObjectIdenticalTo: @ "bada"];
NSLog (@ "index is % lu", index );
// Use arrays to split strings, splice strings,
// String separated by Spaces
NSString * str = @ "zhonger m 19 m ";
NSArray * resultArray = [str componentsSeparatedByString: @ ""];
// Concatenate a string with 66666 characters
NSArray * rArray = @ [@ "bada", @ "qiuxiang", @ "zaiyiqi"];
NSString * str1 = [rArray componentsJoinedByString: @ "66666"];
// As for the variable array, the addition, deletion, and modification operations are provided in the case of the immutable array to make the Array Function more diversified.
// 1. Create a variable array
NSMutableArray * mArr = [[NSMutableArray alloc] initWithCapacity: 0];
// 2. Constructor
NSMutableArray * mArr1 = [NSMutableArray arrayWithCapacity: 0];
// 3. variable array literal creation method
NSMutableArray * mArr2 = [@ [@ "zhonger", @ "dada", @ "zhuzhu", @ "linlin"] mutableCopy];
// Add an element to the variable array, addobject
[MArr2 addObject: @ "addobject"];
// Insert a new element insertObject to the specified position of the variable array
[MArr2 insertObject: @ "charu" atIndex: 1];
// Remove an element from the variable array
// Subscripts
[MArr2 removeObjectAtIndex: 4];
NSLog (@ "mArr2 is % @", mArr2 );
// Specify the content
[MArr2 removeObject: @ "zhuzhu"];
NSLog (@ "mArr2 is % @", mArr2 );
// Delete the last element in the array
[MArr2 removeLastObject];
NSLog (@ "mArr2 is % @", mArr2 );
// Delete all elements in the array
// [MArr2 removeAllObjects];
// NSLog (@ "mArr2 is % @", mArr2 );
// Replace the elements in the variable array
[MArr2 replaceObjectAtIndex: 0 withObject: @ "gou"];
[MArr2 replaceObjectAtIndex: 1 withObject: @ "taixu"];
NSLog (@ "mArr2 is % @", mArr2 );
// Swap the positions of two elements in a variable array
[MArr2 exchangeObjectAtIndex: 0 withObjectAtIndex: 1];
NSLog (@ "mArr2 is % @", mArr2 );
Note: No matter whether it is an immutable or variable array, if it is in the MRC environment, as long as you add this array, it will lead to the reference count plus 1, as one of the three large capacity, arrays store ordered objects. Arrays can be sorted, unlike dictionaries.