In objective-C, nsarray and nsmutablearray are used to define two-dimensional arrays and multi-dimensional numbers.

Source: Internet
Author: User
In objective-C, nsarray and nsmutablearray are used to define two-dimensional arrays and multi-dimensional array directories.
  • Problem description
  • Introduction to Array Pairs in objective-C, such as nsarray and nsmutablearray
  • Two-dimensional array: Nested nsmutablearray
  • Multi-dimensional array: Nested multiple nsmutablearray
Problem description

To be honest, I am not very familiar with object-oriented programming, so I am operatingObjective-CIn the array object, always unableCIn the array to compare, just to create a two-dimensional array, you need to make the corresponding settings according to the subscript of the Two-dimensional array, it is the first time to use the array object, all kinds of unfamiliar, not adaptive... Miss itarray[0][1]It's a pity that,NSArrayAndNSMutableArrayI couldn't do this, so I checked the information for half a day and wrote a few small programs for verification. I finally understood how to do it. The following is a detailed introduction.

Introduction to Array Pairs in objective-C, such as nsarray and nsmutablearray

Objective-CProvides two types of array objects. One is an immutable array.NSArray, One is a variable arrayNSMutableArray.

First, be clearCInArrayAndObjective-CInArray objectDifference in use, the former can be assigned a value=To set the value of an element in an array, for example:

int array[0] = 12;

The latter must be implemented through the method provided in the class.=Assign values inObjective-CWe useFoundationFramework, which isNSArrayAndNSMutableArrayProvides a variety of methods to set array elements.

Here we will discuss variable arrays.NSMutableArray, It usesaddObjectMethod to add array elements. Suppose we want to define a variable array containing four elements and assign values to "1, 2, 3, and 4" respectively to implement codemyTestArray.mAs follows:

#import <Foundation / Foundation.h>

int main (int argc, const char * argv [])
{
   @autoreleasepool
   {
     // initialize the array
     NSMutableArray * myArray = [[NSMutableArray alloc] initWithCapacity: 4];
     // Increase array elements in order
     [myArray addObject: @ "1"];
     [myArray addObject: @ "2"];
     [myArray addObject: @ "3"];
     [myArray addObject: @ "4"];

     // print the contents of the array myArray
     NSLog (@ "% @", myArray);
   }

   return 0;
}

Save the above CodemyTestArray.mAnd then execute the following command in the terminal window to compile:

gcc -framework Foundation -o myTestArray myTestArray.m

Generate executable filesmyTestArrayThe running result is as follows:

Air:gcc-code admin$ ./myTestArray 
2014-08-16 22:26:17.794 myTestArray[75966:507] (
    1,
    2,
    3,
    4
)
Air:gcc-code admin$
Two-dimensional array: Nested nsmutablearray

InObjective-CThere is no intuitive two-dimensional array, but let's recallCIn the two-dimensional array, we will find that the so-called two-dimensional array is just a nest of two one-dimensional arrays, with one-dimensional array as the array element.

With this understanding, it is easy to construct our two-dimensional array object, demonstration programmyD2Array.mThe following code:

#import <Foundation / Foundation.h>

int main (int argc, const char * argv [])
{
  @autoreleasepool
  {
    // Initialize the array as a column, it has 4 elements, we can think of it as 4 columns
    NSMutableArray * myColumnArray = [[NSMutableArray alloc] initWithCapacity: 4];

    // Initialize 4 one-dimensional arrays, each one-dimensional array has 4 elements, we can think of it as 1 row and 4 columns, 4 rows add up to 4 rows and 4 columns
    NSMutableArray * myRowArray1 = [[NSMutableArray alloc] initWithCapacity: 4];
    NSMutableArray * myRowArray2 = [[NSMutableArray alloc] initWithCapacity: 4];
    NSMutableArray * myRowArray3 = [[NSMutableArray alloc] initWithCapacity: 4];
    NSMutableArray * myRowArray4 = [[NSMutableArray alloc] initWithCapacity: 4];

    // Add array elements to each row in turn
    // first row
    [myRowArray1 addObject: @ "11"];
    [myRowArray1 addObject: @ "12"];
    [myRowArray1 addObject: @ "13"];
    [myRowArray1 addObject: @ "14"];

    // second line
    [myRowArray2 addObject: @ "21"];
    [myRowArray2 addObject: @ "22"];
    [myRowArray2 addObject: @ "23"];
    [myRowArray2 addObject: @ "24"];

    // The third row
    [myRowArray3 addObject: @ "31"];
    [myRowArray3 addObject: @ "32"];
    [myRowArray3 addObject: @ "33"];
    [myRowArray3 addObject: @ "34"];

    // fourth line
    [myRowArray4 addObject: @ "41"];
    [myRowArray4 addObject: @ "42"];
    [myRowArray4 addObject: @ "43"];
    [myRowArray4 addObject: @ "44"];

    // Take these 4 one-dimensional arrays as the elements of the column array and add them in order
    [myColumnArray addObject: myRowArray1];
    [myColumnArray addObject: myRowArray2];
    [myColumnArray addObject: myRowArray3];
    [myColumnArray addObject: myRowArray4];

    // Print the contents of the arrays myRowArray1 ~ 2 and myColumnArray respectively
    NSLog (@ "myRowArray1:% @", myRowArray1);
    NSLog (@ "myRowArray2:% @", myRowArray2);

    NSLog (@ "myColumnArray:% @", myColumnArray);
  }

  return 0;
}

Save the above CodemyD2Array.mAnd then execute the following command in the terminal window to compile:

gcc -framework Foundation -o myD2Array myD2Array.m

Generate executable filesmyD2ArrayThe running result is as follows:

Air:gcc-code admin$ ./myD2Array 
2014-08-16 23:12:14.471 myD2Array[76792:507] myRowArray1: (
    11,
    12,
    13,
    14
)
2014-08-16 23:12:14.474 myD2Array[76792:507] myRowArray2: (
    21,
    22,
    23,
    24
)
2014-08-16 23:12:14.474 myD2Array[76792:507] myColumnArray: (
        (
        11,
        12,
        13,
        14
    ),
        (
        21,
        22,
        23,
        24
    ),
        (
        31,
        32,
        33,
        34
    ),
        (
        41,
        42,
        43,
        44
    )
)
Air:gcc-code admin$

Well, the above Code createsObjective-CUseCocoaSo how to access the elements in the two-dimensional array? Can I use the following method to reference it? We can use this methodobjectAtIndexTo obtain elements in the array, for example, to obtain an arraymyRowArray1You can use the following code[Myrowarray1 objectatindex: column subscript]:

[myRowArray1 objectAtIndex:0]
[myRowArray1 objectAtIndex:1]
[myRowArray1 objectAtIndex:2]
[myRowArray1 objectAtIndex:3]

If you want to obtainmyColumnArrayYou can use the following code[Mycolumnarray objectatindex: Row subscript]:

[myColumnArray objectAtIndex:0]
[myColumnArray objectAtIndex:1]
[myColumnArray objectAtIndex:2]
[myColumnArray objectAtIndex:3]

After running the above Code, we will find that what we really need isRow subscriptAndColumn subscriptIt can be used together, because in this way, the final element can be accurately located, instead of an array element, it is in the following form:

[[Mycolumnarray objectatindex: Row subscript] objectatindex: column subscript];

In this wayCIn the programArray [row subscript] [column subscript]The form is similar and easy to understand. The final DEMO codemyD2-2TestArray.mAs follows:

#import <Foundation / Foundation.h>

int main (int argc, const char * argv [])
{
  @autoreleasepool
  {
    // Initialize the array as a column, it has 4 elements, we can think of it as 4 columns
    NSMutableArray * myColumnArray = [[NSMutableArray alloc] initWithCapacity: 4];

    // Initialize 4 one-dimensional arrays, each one-dimensional array has 4 elements, we can think of it as 1 row and 4 columns, 4 rows add up to 4 rows and 4 columns
    NSMutableArray * myRowArray1 = [[NSMutableArray alloc] initWithCapacity: 4];
    NSMutableArray * myRowArray2 = [[NSMutableArray alloc] initWithCapacity: 4];
    NSMutableArray * myRowArray3 = [[NSMutableArray alloc] initWithCapacity: 4];
    NSMutableArray * myRowArray4 = [[NSMutableArray alloc] initWithCapacity: 4];

    // Add array elements to each row in turn
    // first row
    [myRowArray1 addObject: @ "11"];
    [myRowArray1 addObject: @ "12"];
    [myRowArray1 addObject: @ "13"];
    [myRowArray1 addObject: @ "14"];

    // second line
    [myRowArray2 addObject: @ "21"];
    [myRowArray2 addObject: @ "22"];
    [myRowArray2 addObject: @ "23"];
    [myRowArray2 addObject: @ "24"];

    // The third row
    [myRowArray3 addObject: @ "31"];
    [myRowArray3 addObject: @ "32"];
    [myRowArray3 addObject: @ "33"];
    [myRowArray3 addObject: @ "34"];

    // fourth line
    [myRowArray4 addObject: @ "41"];
    [myRowArray4 addObject: @ "42"];
    [myRowArray4 addObject: @ "43"];
    [myRowArray4 addObject: @ "44"];

    // Take these 4 one-dimensional arrays as the elements of the column array and add them in order
    [myColumnArray addObject: myRowArray1];
    [myColumnArray addObject: myRowArray2];
    [myColumnArray addObject: myRowArray3];
    [myColumnArray addObject: myRowArray4];

    // Print the elements in the array according to the row index and column index
    int row;
    int column;

    // Print the elements in the array myRowArray1 by the line index
    for (row = 0; row <4; row ++)
    {
        NSLog (@ "myRowArray1 row index printing:% @", [myRowArray1 objectAtIndex: row]);
    }

    // Print the elements in the array myColumnArray by row index--the elements are one-dimensional arrays
    for (row = 0; row <4; row ++)
    {
        NSLog (@ "myColumnArray row index printing:% @", [myColumnArray objectAtIndex: row]);
    }

    // Print the elements in the array myColumnArray by row index to column index
    for (row = 0; row <4; row ++)
    {
        for (column = 0; column <4; column ++)
        {
            NSLog (@ "myColumnArray row index + column index print:% @", [[myColumnArray objectAtIndex: row] objectAtIndex: column]);

        }
    }

  }

  return 0;
}

The compilation command is the same as above:

gcc -framework Foundation -o myD2-2Array myD2-2Array.m

The execution result is as follows:

Air: gcc-code admin $ ./myD2-2Array
2014-08-16 23: 58: 57.361 myD2-2Array [77579: 507] myRowArray1 line subscript print: 11
2014-08-16 23: 58: 57.364 myD2-2Array [77579: 507] myRowArray1 line subscript print: 12
2014-08-16 23: 58: 57.365 myD2-2Array [77579: 507] myRowArray1 line subscript print: 13
2014-08-16 23: 58: 57.365 myD2-2Array [77579: 507] myRowArray1 line subscript print: 14
2014-08-16 23: 58: 57.366 myD2-2Array [77579: 507] myColumnArray row subscript printing: (
    11,
    12,
    13,
    14
)
2014-08-16 23: 58: 57.366 myD2-2Array [77579: 507] myColumnArray row subscript printing: (
    twenty one,
    twenty two,
    twenty three,
    twenty four
)
2014-08-16 23: 58: 57.366 myD2-2Array [77579: 507] myColumnArray row subscript printing: (
    31,
    32,
    33,
    34
)
2014-08-16 23: 58: 57.367 myD2-2Array [77579: 507] myColumnArray row subscript printing: (
    41,
    42,
    43,
    44
)
2014-08-16 23: 58: 57.367 myD2-2Array [77579: 507] myColumnArray row index + column index print: 11
2014-08-16 23: 58: 57.368 myD2-2Array [77579: 507] myColumnArray row index + column index print: 12
2014-08-16 23: 58: 57.368 myD2-2Array [77579: 507] myColumnArray row index + column index print: 13
2014-08-16 23: 58: 57.368 myD2-2Array [77579: 507] myColumnArray row index + column index print: 14
2014-08-16 23: 58: 57.369 myD2-2Array [77579: 507] myColumnArray row index + column index print: 21
2014-08-16 23: 58: 57.369 myD2-2Array [77579: 507] myColumnArray row index + column index print: 22
2014-08-16 23: 58: 57.370 myD2-2Array [77579: 507] myColumnArray row index + column index print: 23
2014-08-16 23: 58: 57.370 myD2-2Array [77579: 507] myColumnArray row index + column index print: 24
2014-08-16 23: 58: 57.371 myD2-2Array [77579: 507] myColumnArray row index + column index print: 31
2014-08-16 23: 58: 57.371 myD2-2Array [77579: 507] myColumnArray row index + column index print: 32
2014-08-16 23: 58: 57.371 myD2-2Array [77579: 507] myColumnArray row index + column index print: 33
2014-08-16 23: 58: 57.372 myD2-2Array [77579: 507] myColumnArray row index + column index print: 34
2014-08-16 23: 58: 57.372 myD2-2Array [77579: 507] myColumnArray row index + column index print: 41
2014-08-16 23: 58: 57.373 myD2-2Array [77579: 507] myColumnArray row index + column index print: 42
2014-08-16 23: 58: 57.373 myD2-2Array [77579: 507] myColumnArray row index + column index print: 43
2014-08-16 23: 58: 57.373 myD2-2Array [77579: 507] myColumnArray row index + column index print: 44
Air: gcc-code admin $ 
Multi-dimensional array: Nested multiple nsmutablearray

Understand the two-dimensional array above, and useNSMutableArrayIt is easy to construct multi-dimensional arrays, because the principles are the same, as long as an array is defined, suchmyDepthArray, Put the two-dimensional arraymyColumnArrayYou can use it as an element, but it is complicated to write and will not be detailed here.

In addition, this article mainly usesNSMutableArrayFor exampleNSArrayThe implementation of two-dimensional arrays and multi-dimensional arrays is also very similar, interested friends can try to write.

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.