oc--foundation-Common Classes (2)----Nsarray

Source: Internet
Author: User

NsarrayUsed to store an ordered list of objects, which are immutable and cannot store basic data types in C, such as int, float, enum, struct, or nil
Nsarray *array=[nsarray arraywithobject:@ "12345"];
after the array is created, it is immutable and needs to be initialized at the time of creation. when an object is plugged into an array, the object's counter is incremented by 1.   Nsarray Traversal
    • Normal traversal
Nsuinteger count = [array count];        for (int i=0;i<count;i++) {id obj = [array objectatindex:i]; }
    • Fast traversal
for (ID obj in array)
    • Iterative Walker
Nsarray *array=[nsarray arraywithobjects:@ "123", @ "456"]; // gets the iterator for the array, the positive sequence Nsenumerator @eunm =[array objectenumerator]; // gets the next element to traverse ID obj =nil;  while (obj=[enum  Nextobject]) {  NSLog (@ "obj=%@", obj);}

Reverse iterator: Nsenumerator *enum=[array reverseobjectenumerator];

    • Block traversal
[Array enumerateobjectsusingblock:^ (ID object,nsuinteger index,bool *stop)        {NSLog (@ "%@-%zi", Object,index); }]
    • Iterator traversal: Gets all the elements in the array nsarray *array = [enumerator allobjects];
AllObjects is to remove objects that have not been traversed     Nsarray derivation of a new collection
    • -(Nsarray *) Arraybyaddingobject: (ID) anobject
Add an element that returns a new Nsarray (the method caller itself has not changed)
    • -(Nsarray *) Arraybyaddingobjectsfromarray: (Nsarray *) Otherarray
Add all elements of Otherarray and return a new Nsarray (the method caller itself has not changed)
    • -(Nsarray *) Subarraywithrange: (nsrange) Range
Array elements that intercept range ranges Nsarray Other methods
    • -(NSString *) componentsjoinedbystring: (NSString *) separator
Concatenation of array elements into a string using separator as a concatenation character
    • -(BOOL) WriteToFile: (NSString *) path atomically: (BOOL) Useauxiliaryfile
Persist a nsarray to a file If you want to read the contents of a file into an array, the contents of the file must conform to a certain format nsmutablearray (variable group)
    • A variable Nsarray,nsarray subclass that can optionally add or remove elements
    • Ways to create Nsmutablearray
Nsmutablearray *array=[nsmutablearray arraywithobject:@ "1"]; // adding elements [Array addobject:@ "2"]; // Delete Element [Array removeobject:@ "1"]; // Delete last element [Array Removelastobject] // Delete all elements [Array removeallobjects];
You can also use the Create Nsarray method to create a Nsmutablearray
    • When an element is added to a collection, a retain operation is performed, and when an element is removed from the collection, a release operation is performed, and when the collection is destroyed (Dealloc is called), All elements in the collection perform a release operation once (this principle also applies to other collections: Nsictionary\nsset, etc.)
Sorting method Reference Nsarray Sort, sort the method that is called by the return value of a variable group, using a method with no return value, using a method with a return value for an immutable group  

oc--foundation-Common Classes (2)----Nsarray

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.