[OC learning-18] Simple induction of nsarray initialization and common Array Operations

Source: Internet
Author: User

The array is also divided into mutable and immutable. Like nsstring, the initialization method is similar, and the operations on elements are similar, as shown below.

Practice several times more and know what each means. Make a deeper impression when developing a project.

// Immutable array // the element of the array can only be an object. If it is an int or float, you can use nsnumber to convert it to use // initialization methods, nsarray * arr1 = [nsarray arraywithobject: @ "hello"]; nsarray * arr2 = [nsarray arrayrayarraywithobjects: @ "hello", @ "world", nil]; nsarray * arr3 = [nsarray arraywitharray: arr2]; nsarray * arr4 = [nsarray arraywithcontentsoffile: <# (nsstring *) #>]; nsarray * arr5 = [nsarray arraywithcontentsofurl: <# (nsurl *) #>]; // Of Course, the most basic but complex Initialization is like this nsarray * arr6 = [[nsarray alloc] initwithobjects: @ "hello", @ "world", nil]; // obtain the number of arrays nsinteger COUNT = [arr2 count]; // access the object nsstring * arrobj = [arr2 objectatindex: 2] According to the subscript; // add or remove new elements (objects) to the array nsarray * arrnew = [arr2 arraybyaddingobject: @ "yes"]; // concatenate the elements in the array into a string nsstring * strnew = [arr2 componentsjoinedbystring: @ "-"] using connectors; // query whether the array contains a certain element, yes and no bool iscontain = [arr2 containsobject: @ "hello"]; // query and return the location. If not, nsnotfound nsinteger Index = [arr2 indexofobject: @ "hello"]; // obtain the last element nsstring * lastele = [arr2 lastobject]; // variable array, five elements nsmutablearray * mutarr = [[nsmutablearray alloc] initwithcapacity: 5]; // Add an element [mutarr addobject: @ "hell"]; // insert an element at a specified position [mutarr insertobject: @ "word" atindex: 1]; // remove the elements from the array. [mutarr removelastobject], [mutarr removeobject: @ "hell"], [mutarr removeobjectatindex: 0], and [mutarr removeallobjects] by name. // replace element [mutarr replaceobjectatindex: 1 withobject: @ "hello"]; // Add an array to another [mutarr addobjectsfromarray: arr1]; // application: array Element Traversal method nsarray * arr7 = [nsarray arraywithobjects: @ "A", @ "B", @ "C", nil]; int length = [arr7 count]; for (INT I = 0; I <length; I ++) {nsstring * element = [arr7 objectatindex: I]; nslog (@ "% @", element );} // application: traverse the Array Element Method for (nsstring * str8 in arr7) {nslog (@ "% @", str8);} // use the ID when the element type is unknown, for example, in addition to nsstring, nsnumber for (ID str8 in arr7) {nslog (@ "% @", str8 );}


[OC learning-18] Simple induction of nsarray initialization and common Array Operations

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.