Foundation framework--nsarray class, array common operations

Source: Internet
Author: User

An array is an ordered set of objects that cannot hold basic data types, only instances (objects) of the class Nsarray *array1=[nsarray arraywithobject:@ "one"];Nsarray *array2=[nsarray arraywithobjects:@ "One", @ "one", Nil];nsarray *array3=[nsarray Arraywitharray:array2];(2 and 3 point to the same address)Nsarray *array4=[nsarray Arraywithcontentsoffile:path];Nsarray*array5=[[nsarray alloc]initwitharray:@ "one"];
adds an element to the end of the array by adding an object to the arraynsarray *array5 =[array2 arraybyaddingobject:@ "End"];
links the elements in an array with the specified stringNsstring*string1=[array componentsjoinedbystring: ","];
whether the array contains the specified elementBOOL iscontain=[array containsobject:@ "BBB"];
queries the elements of the specified object in the array, (returns index subscript) If this element is not, return NsnotfoundNsinteger index=[array indexofobject:@ "CCC";nsstring *lastobject=[array5 Lastobject];
Initialize, set the number of elements to 5, but you can change theNsmutablearray *mutablearray=[nsmutablearray Arraywithcapacity:5];
add an element to an array[Mutablearray addobject:@ "AAA"];
inserts an element into an array by specifying the subscript[Mutablearray insertobject:@ "CCC" atindex:0];
remove the last element[Mutablearray Removelastobject];
removing the specified element[Mutablearray removeobject:@ "AAA"];
Remove the element that made the subscript[Mutablearray removeobjectatindex:0]
adds an array to the array;[Mutablearray addobjectsfromarray:array100];array100 is a subset of Mutablearray
replaces the specified following table element[Mutablearray replaceobjectatindex:0 withobject:@ "Replace"];
Remove all elements[Mutablearray removeallobjects];
traversal of an arrayIt's best to put the same type in an array

Nsarray *array=[nsarray arraywithobjects:@ "1", @ "11", @ "111"

, @ "1111", @ "11111", nil];

for (int i=0; I<[array count]; i++) {

NSString * Str1=[array OBJECTATINDEX:I];

NSLog (@ "%@", str1);

} (generally not due to poor performance)

For (NSString *string in array)

{NSLog (@ "Found%@,string");}For (ID string in array) {NSLog (@ "found%@,string");an ID is used when an array element type is not determined

Foundation framework--nsarray class, array common operations

Related Article

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.