------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
How to create a------------Nsarray
1), class method creation
+ (instancetype) array;
+ (Instancetype) Arraywithobject: (ID) anobject;
+ (Instancetype) Arraywithobjects: (ID) firstobj, ...;
+ (Instancetype) Arraywitharray: (Nsarray *) array;
+ (ID) arraywithcontentsoffile: (NSString *) path;
+ (ID) Arraywithcontentsofurl: (nsurl *) URL;
2), object method creation
-(Instancetype) Initwithobjects: (ID) firstobj,;
-(Instancetype) Initwitharray: (Nsarray *) array;
-(Nsarray *) Initwithcontentsoffile: (NSString *) path;
-(Nsarray *) Initwithcontentsofurl: (nsurl *) URL;
Nsarray Features: //Once created, content cannot be changed //can only hold OC Object //1) Create an empty array nsarray *arr1 = [Nsarray array]; 2) Create an array with only one element nsarray *arr2 = [Nsarray arraywithobject:@ "1"]; 3) Create an array with multiple elements //nil representing the end of array assignment //Common wording nsarray *ARR3 = [Nsarray arraywithobjects:@ "one", @ "one", @1, nil];< C11/>nslog (@ "ARR3 =%@", ARR3); 4) Call the object method, create the array //nil nil NULL NSNULL nsarray *arr4 = [[Nsarray alloc] initwithobjects:@ "three", [ NSNull null],@ "Four", nil]; NSLog (@ "ARR4 =%@", ARR4); 5) Use an array to create another array nsarray *ARR5 = [Nsarray ARRAYWITHARRAY:ARR3]; NSLog (@ "ARR5 =%@", ARR5);
3), common usage
-(Nsuinteger) count; Get the number of collection elements
-(ID) Objectatindex: (Nsuinteger) index; Gets the element of the index position
-(BOOL) Containsobject: (ID) anobject; contains an element
-(ID) lastobject; Gets the last element
-(ID) firstobject; Gets the first element
-(Nsuinteger) Indexofobject: (ID) anobject; Find the position of the AnObject element in the array
Nsarray *ARR3 = [Nsarray arraywithobjects:@ "one", @ "one", @1,@ "three", nil]; NSLog (@ "ARR3 =%@", ARR3); 1) Gets the length of the array count NSLog (@ "%ld", Arr3.count) for the number of elements in the array; 2) According to subscript, get subscript corresponding to the object NSLog (@ "%@", [Arr3 Objectatindex:3]); 3) Return the subscript of the element Nsuinteger loc = [Arr3 indexofobject:@ "three"]; NSLog (@ "%ld", loc); 4) If the array contains an element if ([Arr3 containsobject:@ "Four"]) { NSLog (@ "contains this element"); } else{ NSLog (@ "does not contain");} }
Shorthand form for-------------Nsarray
Nsarray *arr = @[@ "Jack" @ "Rose" @ "Jim"];
Format:
Nsarray *arr = @[array element];
Nsarray *arr = @[@ "1", @ "one", @ "3", @4,@ "one"];
NSLog (@ "arr =%@", arr);
get the element format:
arr[element angle Mark index];
NSString *str = arr[0];//gets a 0th element
nsstring *str =[arr objectatindex:2];
NSLog (@ "%@", str);
2) access to array elements in a simplified manner
str = arr[1]; array element access in C language form
NSLog (@ "%@", str);
Traversal of the-------------------Nsarray
Nsarray *arr = @[@ "One", @ "one", @ "three", @ "four"];
1), normal for loop, access by subscript
for (int i=0; i<arr.count; i++) { NSLog (@ "-%@", Arr[i]); }
2), enhanced for loop (for in)
for (NSString * str in arr) { NSLog (@ "--->%@", str); }
3), using block mode, to traverse
Block three parameters meaning: 1, array element 2, element subscript 3, whether stop [arr enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) { if (idx = = 2) { *stop = YES; Stop /break; } else{ NSLog (@ "idx =%ld,obj =%@", idx,obj);
--------------Nsarray Read and write files
1), write the array elements to the file
-(BOOL) WriteToFile: (NSString *) path atomically: (bool) useauxiliaryfile;
Code:
Nsarray *arr = [Nsarray arraywithobjects:@ "1", @ "2", @ "3", @ "4", @ "5", nil]; Write data to the plist file if ([Arr writetofile:@ "/users/liwei/desktop/arr.plist" Atomically:yes]) { NSLog (@ "Write succeeded!"); }
2), reading an array from a file
+ (Nsarray *) Arraywithcontentsoffile: (NSString *) path;
Code:
Nsarray *readarr = [Nsarray arraywithcontentsoffile:@ "/users/zhaoxiaohu/desktop/arr.xml"];
---------------------Nsarray and strings
1), link the array elements into a string (Nsarray method)
-(NSString *) componentsjoinedbystring: (NSString *) separator;
This is the Nsarray method, using separator as the concatenation character to stitch the array elements into a string
Define an array nsarray *arr = @[@1,@2,@3,@4]; 1) Requirements: Connect the elements in the array with "-" / [array componentsjoinedbystring @ "delimiter"]; 1-2-3-4 nsstring *str = [arr componentsjoinedbystring:@ "-"]; NSLog (@ "str =%@", str);
2), String segmentation method (NSString method)
Define an array nsarray *arr = @[@1,@2,@3,@4]; 1) Requirements: Connect the elements in the array with "-" / [array componentsjoinedbystring @ "delimiter"]; 1-2-3-4 nsstring *str = [arr componentsjoinedbystring:@ "-"]; NSLog (@ "str =%@", str);
2) Give a string, split into an array
400-800-12580 //Get 12580 nsstring *str2 = @ "400-800-12580"; Nsarray *ARR2 = [str2 componentsseparatedbystring:@ "-"]; NSLog (@ "%@", [arr2 Firstobject]); NSLog (@ "%@", [arr2 Lastobject]); NSLog (@ "%@", arr2[1]); STR2 = @ "400-800-12580#400-888-11200"; Nsarray *ARR3 = [str2 componentsseparatedbystring:@ "#"]; NSLog (@ "ARR3 =%@", ARR3); Each part of the first call nsarray *ARR4 = [[arr3 Firstobject] componentsseparatedbystring:@ "-"]; NSLog (@ "ARR4 =%@", ARR4); Nsarray *ARR5 = [[arr3 Lastobject] componentsseparatedbystring:@ "-"];
-----------------------Nsmutablearray--------------------
1,nsmutablearray Introduction
1), Nsmutablearray is a subclass of Nsarray
2), Nsarray is immutable, once the initialization is complete, the contents of it will always be fixed, can not delete the elements inside, and can no longer add elements inside.
3), Nsmutablearray is variable, can add \ change \ delete element at any time
2,nsmutablearray Basic Use Method
1), create an array
Create an empty array nsmutablearray *arr = [Nsmutablearray array]; Create an array, and specify a length of 5, at this time also an empty array nsmutablearray *arr2 = [[Nsmutablearray alloc] initwithcapacity:5]; Create an array containing two elements nsmutablearray *arr3 = [Nsmutablearray arraywithobjects:@ "1", @ "2", nil]; Call object methods to create an array nsmutablearray *ARR4 = [[Nsmutablearray alloc] initwithobjects:@ "1" @ "2", nil];
2), adding elements
-(void) AddObject: (ID) object; Add an Element
[Arr1 addobject:@ "Fengjie"];
3), insert Element
-(void) InsertObject: (ID) anobject atindex: (nsuinteger) index;
4), delete element
-(void) removeallobjects;//Delete all
-(void) removelastobject;//Delete the last
-(void) Removeobjectatindex: (nsuinteger) index;//Delete specified location
-(void) Removeobject: (id) anobject;//Delete the specified element
Delete // [arr1 removeobject:@ "ZBZ"] based on object content; Delete according to location // [arr1 removeobjectatindex:1]; Delete all [arr1 removeallobjects];
5), modify the element
-(void) Replaceobjectatindex: (nsuinteger) index Withobject: (ID) anobject;
[Arr3 replaceobjectatindex:1 withobject:@ "Four"];//change the subscript element to four //simpler method arr3[1] = @ "Five";
6), find the element
-(BOOL) Containsobject: (ID) anobject;
View Array Arr3 There is no four in this element// BOOL issearch = [Arr3 containsobject:@ "four"];// NSLog (@ "%d", issearch);
7), Exchange element
Swapping elements for idx1 and IDX2 positions
-(void) Exchangeobjectatindex: (Nsuinteger) idx1 Withobjectatindex: (Nsuinteger) idx2;
Nsmutablearray *arr5 =[nsmutablearray arraywithobjects:@1,@2,@3,@4,@5, nil]; The elements of the array element//subscript 0 can be exchanged with the element of subscript four [Arr5 exchangeobjectatindex:0 withobjectatindex:4]; NSLog (@ "%@", ARR5);
-----------------Nsmutablearray Error usage
Nsmutablearray *array = @[@ "Bob", @ "Steve", @ "John"];//this quote warns
Use @[@ "Bob", @ "Steve", @ "John" This way is always created by Nsarray (immutable group);
Correct usage:
Nsmutablearray *array = [Nsmutablearray arraywitharray:@[@ "Bob", @ "Steve", @ "John"];
The use of Nsarray and Nsmutablearray in the framework of Dark Horse programmer--obbjective-c-foundation-my finishing