Question

I want to add data into my array similar way as this C++ code.

String a[3][4] = {  
    {"Burger", "6", "b", "u"} ,   /*  initializers for row indexed by 0 */
    {"Pizza", "5", "p", "z"} ,   /*  initializers for row indexed by 1 */
    {"Kebab", "4", "k", "b"}   /*  initializers for row indexed by 2 */
};

In objective-C, my attempt.

NSString *a[10][20] = {
    {@"Burger", @"6", @"b", @"u"} ,   /*  initializers for row indexed by 0 */
    {@"Pizza", @"5", @"p", @"z"} ,   /*  initializers for row indexed by 1 */
    {@"Kebab", @"4", @"k", @"b"}   /*  initializers for row indexed by 2 */
};

I am not sure if above example works, I will test it once I access to a MAC.

My question is:

What is the right way of doing it, perhaps the best way.

And will my Objective-C code work?

I apologize for asking this question without testing it due to no-access to a MAC.

Was it helpful?

Solution

In Objective-C you want to declare a like this:

NSArray *a = @[
    @[@"Burger", @"6", @"b",@"u"] ,   /*  initializers for row indexed by 0 */
    @[@"Pizza", @"5", @"p", @"z" ] ,   /*  initializers for row indexed by 1 */
    @[@"Kebab", @"4", @"k", @"b"]   /*  initializers for row indexed by 2 */
];

Then you can index into a to get each array like this:

NSArray *firstArray = a[0];

OTHER TIPS

Using Objective-Cs literal syntax for an array with 3 arrays containing NSStrings:

NSArray *a = @[
               @[@"Burger", @"6", @"b", @"u"],
               @[@"Pizza", @"5", @"p", @"z"],
               @[@"Kebab", @"4", @"k", @"b"]
              ];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top