Explain:
The Nsarray class has two restrictions. First, it can only store objective-c objects, not the original C-language underlying data types, such as the random pointers in int, float, enum, struct, and Nsarray. Also, you cannot store nil (0 or NULL values of objects) in Nsarray. There are a number of ways to avoid these limitations.
You can arraywithobjects by using the class method: Create a new nsarray. Send a comma-delimited list of objects, adding nil at the end of the list to the end of the list (this is one reason that you cannot store nil in the array).
NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three"nil
This line of code creates an array of 3 elements consisting of a nsstring literal object. You can also use the array literal format to create an array that is very similar to the nsstring literal format, except that the square brackets are used instead of the quotation marks, as shown below.
NSArray *array2 = @[@"one", @"two", @"three"];
Although the array and Array2 objects are different, their contents are the same, and the latter has significantly less input than the previous one.
Note: You do not have to intentionally complement nil at the end when using literal syntax.
On the code:
/* * Nsarray (non-variable group) */ //define objects for the Nsarray class Nsarray*array1 = [[NsarrayAlloc] initwithobjects:@"1", @2, @"Good", @"ABCdef",Nil] ;NSLog( @"%@", array1);NSLog( @"%@", [array1 description]);Nsarray*array2 = [Nsarrayarraywithobjects:@"1", @3, @"??",Nil] ;NSLog( @"%@", array2);//array of syntactic sugar forms (literal, literal) Nsarray*array3 = @[@"1", @3, @"? (~"~) ~zz"] ;NSLog( @"%@", ARRAY3);//Gets the number of array elements NsintegerCount = [Array3 count];NSLog( @"%ld", count);//Get the corresponding object by subscript for(inti =0; I < count; i++) {NSLog( @"%@", [Array3 objectatindex:i]);NSLog( @"%@", Array3[i]); }//object to find its corresponding subscript in the array NsintegerIndex =[ARRAY3 indexofobject:@3] ;NSLog( @"%ld", index);NSString*strfile = [NSStringstringwithcontentsoffile:@"/users/lanouhn/desktop/words.txt"Encoding:nsutf8stringencoding Error:Nil] ;NSLog( @"%@", strfile);//Componentsseparatedbystring The original string into multiple substrings by the given string and saves it in the array returned Nsarray*array4 = [Strfile componentsseparatedbystring:@" "] ;NSLog( @"%@", Array4);/* * NSMULTABLEARRAY (variable group) */ Nsmutablearray*mutablearray1 = [[NsmutablearrayAlloc] initwitharray:array1];NSLog( @"%@", mutableArray1);Nsmutablearray*mutablearray2 = [NsmutablearrayARRAYWITHARRAY:ARRAY1];NSLog( @"%@", mutableArray2);//Add element[MutableArray2 addobject:@ -] ;NSLog( @"%@", mutableArray2);//Insert Element[MutableArray2 insertobject:@"Insert"Atindex:2] ;NSLog( @"%@", mutableArray2);//Replace element[MutableArray2 Replaceobjectatindex:2withobject:@ -] ;NSLog( @"%@", mutableArray2);//Exchange element[MutableArray2 Exchangeobjectatindex:2Withobjectatindex:3] ;NSLog( @"%@", mutableArray2);//delete (move) except for the last element[MutableArray2 Removelastobject];NSLog( @"%@", mutableArray2);//Delete (move) the element in addition to the specified subscript[MutableArray2 Removeobjectatindex:0] ;NSLog( @"%@", mutableArray2);//Delete (move) all objects except[MutableArray2 removeallobjects];NSLog( @"%@", mutableArray2);
Objective-c----Nsarray, Nsmutablearray