My OC Growth path (an array of review)

Source: Internet
Author: User

Attention:

1. Because the array and the dictionary can only hold object types. So the general value is not directly deposited, can only be converted into object elements to deposit!

2.

Nsarray for an ordered collection of objects

Nsset for Object unordered collection

Nsdictionary for key-value mappings

3. All elements in the array cannot be basic data types, otherwise they will compile an error if they need to be encapsulated into the NSNumber class or the Nsvalue class.

4. The array can be output directly as an object in NSLog, and it is recommended to override the NSObject-(NSString *) Description method.

5. Objects in the array that can store custom classes are recommended to override NSObject-(BOOL) IsEqual: (ID) The object method

First, Nsarray

1. function

nsarray* array = [Nsarray arraywithobjects:@ "ABCDE", @ "Fghij", @ "Klmno", @ "Pqrst", @ "Uvwxy", nil]; NSLog (@ "first element:%@", [array objectatindex:0]); NSLog (@ "element with index 1:%@", [array objectatindex:1]); NSLog (@ "last element:%@", [array lastobject]);//1. Gets the new collection of indexes from the 2~5 elements nsarray* arr1 = [array objectsatindexes: [Nsindexset Indexsetwithindexesinrange:nsmakerange (2, 3)]; NSLog (@ "%@", arr1);//2. Gets the position of the element in the collection NSLog (@ "Klmno location:%ld", [Array indexofobject:@ "Klmno"]);//3.  Gets the position of the element in the specified range of the collection NSLog (@ "Pqrst in 2~5 Range:%ld", [Array indexofobject:@ "Pqrst" Inrange:nsmakerange (2, 3)]); ①//4. Append an element to the last array. The original Nsarray itself did not change, just to assign the newly returned nsarray to Arrayarray = [array arraybyaddingobject:@ "Zzzzz"];//5. Append all the elements of another array to the end of the array. The original Nsarray itself does not change, just assigns the newly returned nsarray to Arrayarray = [array Arraybyaddingobjectsfromarray:[nsarray arraywithobjects:@ " AAAAA ", @" BBBBB ", nil]];for (int i = 0; i < Array.count; i++) {//nslog (@"%@ ", [array objectatindex:i]);//The above code can also be abbreviated as Under Code NSLog (@ "%@", Array[i]);} 6. Get all the elements indexed in array arrays at 5~8 nsarray* arr2 = [Array Subarraywithrange:nsmakerange (5, 3)];//7. Writes the elements of the Nsarray collection to the file [arr2 writetofile:@ "MyFile.txt" atomically:yes]; 

2.for in Fast Enumeration

Reads the file written to disk previously, initializes the Nsarray collection with the contents of the file nsarray* array = [Nsarray arraywithcontentsoffile:@ "MyFile.txt"];for (ID object in Array) {NSLog (@ "%@", object);}

Second, Nsmutablearray

Defines a function that converts the Nsarray collection to a string nsstring* nscollectiontostring (nsarray* array) {nsmutablestring* result = [ Nsmutablestring stringwithstring:@ "["];for (ID obj in array) {[Result appendstring:[obj description]];[ Result appendstring:@ ","];} Gets the string length Nsuinteger len = [result length];//removes the last two characters of the string [result Deletecharactersinrange:nsmakerange (Len-2, 2)]; [Result appendstring:@ "]"];return result;} int main (int argc, char * argv[]) {@autoreleasepool {//reads the file previously written to disk, initializes the Nsmutablearray collection with the contents of the file nsmutablearray* array = [ Nsmutablearray arraywithcontentsoffile:@ "MyFile.txt"];//add an element to the collection [array addobject:@ "ABC"]; NSLog (@ "After the last append of an element:%@", nscollectiontostring (array));//Use Nsarray to add multiple elements to the end of the collection [array addobjectsfromarray: [Nsarray arraywithobjects:@ "Zhang Fei", @ "Guan Yu", Nil]]; NSLog (@ "Last append two elements:%@", nscollectiontostring (array));//Inserts an element into the specified position of the collection [array insertobject:@ "BCD" atindex:2]; NSLog (@ "after inserting an element at index 2:%@", nscollectiontostring (array));//use Nsarray to insert multiple elements into the collection at the specified location [array insertobjects: [Nsarray arraywithobjects:@ "Wu Song", @ "Forest", Nil] atindexes:[nsindexset Indexsetwithindexesinrange:nsmakerange (3,2)]; NSLog (@ "After inserting multiple elements:%@", nscollectiontostring (array));//delete the last element of the collection [array removelastobject]; NSLog (@ "Delete last element:%@", nscollectiontostring (array));//delete the element at the specified index in the collection [array removeobjectatindex:5]; NSLog (@ "Delete the element at index 5:%@", nscollectiontostring (Array)),//delete the element at 2~5 [array removeobjectsinrange:nsmakerange (2, 3)]; NSLog (@ "Delete the element at index 2~5:%@", nscollectiontostring (array));//replace the element at index 2 [array replaceobjectatindex:2 withobject:@] FGH "]; NSLog (@ "replaces the element at index 2:%@", nscollectiontostring (Array));}

  

My OC Growth path (an array of review)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.