1

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.

4
  • So you posted a question without actually trying it first? Why? Commented Apr 27, 2014 at 17:44
  • I already stated that I don't have access to Xcode at the moment. And I need to hardcode huge amount of data right now.. Commented Apr 27, 2014 at 17:46
  • 1
    How much data? It may be better to put the data in a plist file or a database. Hardcoding so many string literals into the code isn't the best option. And you might also wish to use an array of dictionaries. It will make your data a lot more flexible. Commented Apr 27, 2014 at 17:49
  • @rmaddy About 100 rows, that will NEVER be changed. Each row will have different amount of columns, so I don't think database will be a good option. But I will surely check out plist and array of dictionary which I assume is NSmutableDictionary? Commented Apr 27, 2014 at 18:09

2 Answers 2

2

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];
Sign up to request clarification or add additional context in comments.

9 Comments

This will be very inconvenient when I have 30+ rows. Is there a easier way where I can access it like [][]? Thanks for the help.
@Alice Why do you think this syntax is very inconvenient compares to the C-array syntax?
@Alice If I follow your question, then yep you can say NSString *p = a[1][2];
@rmaddy Let's say I want to pick a random word from my Array. with [][] I can go: randomWord = arc4random() % 30; assuming I have 30 rows, this will pick one random row for me. With what @carl suggested I have no idea how to do that. That would be more like picking a random Array instead of a random index.
@Alice Actually there is no difference. In both cases you need to pass two indexes. The 1st gives the row and the 2nd gives the element in that row.
|
1

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"]
              ];

3 Comments

This creates NSArray with NSArray. That's not the same as creating a C-array.
True, though it may serve the purpose to the person asking the question as the question says similar to a C-array.
The question is similar to a C++ C-array. Objective-C is a superset of C so it also supports C-arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.