Nsarray and nsmutablearray array and variable array creation and traversal Review

Source: Internet
Author: User
Tags strtok

 

1. nsarray is a parent class, And nsmutablearray is its subclass. They constitute an array of OC.


2. Create nsarray


Nsarray * array = [[nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "four good", nil];
// Initialize an array with an object, which is any four objects, not necessarily strings.
// The array in OC is not a real array. It is a linked list. The function of Nil is to indicate the end.


3. Print nsarray Traversal


In C language, we want to print an array and use loops for output, but in OC we can print it directly without passing through loops.
Nslog (@ "% @", array); // you can print the content of the array directly. If the printed string contains more than one word, double quotation marks are added when the string is output. The output is as follows:

 

{One, two, three, "four good"} // OC self-added curly braces to indicate that this is to print an array.

 


4. initialize another array with an Array


Nsarray * array2 = [[nsarray alloc] initwitharray: array1];
// Use array 2 to initialize array 1. Elements in array 1 are the same as those in array 2.
// Each element of the array is of any object ID type. Because the array is actually a linked list, each element type of the array is not required to be identical.


5. Like nsstring, alloc init corresponds to an arraywithobjects version.
Nsarray * array1 = [nsarray arraywithobjects: @ "1", @ "2", @ "3", @ "4", nil];


6. In oC, you can use elements in the range of an array to generate another array, as shown below:
Nsarray * array1 = [array objectsatindexes [nsindexset indexsetwithindexesinrange: nsmakerange (1, 3)];
// The element of the set is three elements starting from 1. The red letter function returns an object of A number set. The element of the set is 1, 2, 3,


6. Find a node in the array
When an element is removed from an array, retain is required. Because the array may be variable, delete or other operations may be performed in other cases, so that the object cannot be referenced. Release is also required after use.
Nsarray * STR = [[array objectatindex: 1] retain];
// Obtain the string with the array position 1.


7. Powerful functions of Traversal


(1) enumeration method (the object of the input class is used as an array element)
Nsenumerator * enumerator = [array objectenumerator]; // create an enumerator for an array object
Id OBJ; // create an ID object
While (OBJ = [enumerator nextobject]) {
Nslog (@ "% @", OBJ );}

// For the first loop, the nextobject function returns the element 0 of the array, the second returns 1, and so on.
The enumeration method can implement reverse traversal, Which is irreplaceable by the rapid enumeration method.
Nsenumerator * enumerator = [array reverseobjectenumerator];
Apart from creating enumerators, there is no difference between them and the forward sequence.


(2) Quick Enumeration
Compared with the enumerator method, the quick enumeration method is more concise.
For (id obj in array)
{
Nslog (@ "% @", OBJ );
} // For the first loop, obj is the address of node 0, and the second is the address of Node 1.


8. nsmutablearray variable array. The array elements are variable, similar to nsmutablestring. Because of its variability, you can perform operations on each section.
Nsmutablearray * array = [[nsmutablearray alloc] initwithobjects: @ "one", @ "two", @ "three", nil];
// The declaration of an array is similar to that of an ordinary array. However, you can add or delete nodes to an array.


8. Add a node
[Array addobject: @ "four"]; // it is very easy to add a node, without the distinction between strings.


9. delete a node
[Array removeobjectatindex: 1]; // Based on the index, @ "two" is deleted"
[Array removeobject: @ "Four"]; // Delete the corresponding node based on the address of a node
[Array removeobject: [array objectatindex: 0] inrange: nsmakerange (1, 4)];
// Delete the node with the same address as the address 0 in a certain range ()
[Array removeobject: @ "one" inrange: nsmakerange (2, 3)];
// Delete a node with the same address as @ "one" in three nodes starting from 2
// In the range of (2.3), find @ "one" and delete it.
The last two functions work the same, but they are written differently.


10. nsstring Segmentation
Splits the string and saves the string to a dynamic array at a time. Similar to strtok in C.
Nsstring * STR = @ "I Am a so bad man .";
Nsarray * array = [STR componentsseparatedbystring: @ ""];
// Use spaces as the standard to separate strings. After the operation, the original string remains unchanged, but a bunch of strings are generated and packed in an array for the purpose of segmentation.
Strtok () has a powerful role, which is incomparable to this function. strtok (P, ",") can be separated by spaces and "," to separate strings, the above functions cannot achieve this purpose. We need character collection to solve this problem.
Nsarray * array = [STR componentsseparatedbycharactersinset: [nscharacterset charactersetwithcharactersinstring: @ ","];
// The red part creates a string set and uses the string set as the parameter for segmentation.


11. String concatenation
Splits a string. You can also splice the separated string.
Nsstring * str2 = [array componentsjoinedbystring: @ "];
// Concatenate the strings in the array and separate them with spaces to form a new str2 string.


12. String exchange
[Array exchangeobjectatindex: 0 withobjectatindex: array. Count-1];
// Exchange the nodes at two locations. array. Count-1 indicates the last node.


14. array sorting
[Array sortusingselector: @ selector (oldthandog :)];
// The oldthandog function is a bool function. In fact, yes or no is returned to control sorting.
// This function can be sorted, but does not know the sorting criterion. The bool type function here is actually a self-written function, which specifies the sorting principle through the return value.


14. seletor Selector
Sel variables can actually be seen as a function pointer.
We can abstract functions into sel variables to meet our needs.
When writing code, you cannot determine which function is referenced in a part of the code. We can reference the SEL variable [DOG runner mselector: sel] to call the function, here, Sel is the result of abstracting sel from a function.
For sel with parameters, we will handle this.
[STR example mselector: sel withobject: str2];
// The red part is the processing method of the function with the SEL parameter.

This article is from the "codertodeveloper" blog, please be sure to keep this source http://rongchengfei.blog.51cto.com/6269699/1086045

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.