1

I am trying to make a 2d array in objective-c and I don't really want to use NSArray because I'm using int and the code would annoying: {[array objectAtIndex:x] objectAtIndex:y], not to mention I would have to convert the numbers back from NSNumber... Seems like a lot of extra work.

Can't I do the following?

// .h file
int aTiles[10][2];

// .m file
aTiles = {
        { 0,  0}, // 0
        { 0,  1}, // 1
        { 1,  5}, // 2
        { 0,  0}, // 3
        { 0,  0}, // 4
        { 0,  0}, // 5
};

it works together in the same line (int a[x][x] = {...};), but I need the array to be public so I can access it from any function.

The second line says expecting semicolon.

Thanks

Mykola
  • 3,343
  • 6
  • 23
  • 39
GameMaker
  • 21
  • 3

4 Answers4

0

Looks like you have an extra comma near //5

Do you need to declare a type for aTiles?

 int aTiles = ...
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
0

Since this has also been tagged C++, you can use an ivar:

std::vector<std::vector<int> > tiles;

Then you just resize and set initial element values in your object's initializer.

Otherwise, is this a global or an ivar? should it be const or mutable?

justin
  • 104,054
  • 14
  • 179
  • 226
  • meh have no idea what that is lol. I dont think that xcode can use this. It supports C++, but not to that extent. – GameMaker Jan 29 '12 at 16:26
  • uhhh..... that's a typical 2D array in C++ - Xcode, GCC, and Clang all know what that is. If they could not support this, they just could not say they have modern C++ support :) – justin Jan 29 '12 at 16:32
0

I got slightly different errors when I tried your original code, but this worked:

// .h file
extern int aTiles[10][2];

// .m file
int aTiles[10][2] = {
        { 0,  0}, // 0
        { 0,  1}, // 1
        { 1,  5}, // 2
        { 0,  0}, // 3
        { 0,  0}, // 4
        { 0,  0}, // 5
};
iccir
  • 5,078
  • 2
  • 22
  • 34
  • It does work when you use extern, but when you try and use the array you get error: Undefined symbols for architecture i386: "_aTiles", reference from: [LevelMaster loadLevel] in LevelMaster.o note loadLevel is the function where i tried NSLog(@"test %i", aTiles[3][0]); – GameMaker Jan 29 '12 at 16:22
  • *when you use the array in another function you get that error, you can use it in the function you defined it in – GameMaker Jan 29 '12 at 16:31
  • Is LevelMaster in Objective-C or Objective-C++ (.m or .mm)? What about the extern and declaration of aTiles? Obj-C or Obj-C++? – iccir Jan 29 '12 at 22:18
  • They are objective-c, the xcode default, but you can use c/c++ methods i.e. int array[x][x]. – GameMaker Jan 30 '12 at 00:26
  • I'd move to chat, but can't due to karma - What two files did you place my examples above in? Based on your error, I'm assuming they are in something other than LevelMaster.h / LevelMaster.m? – iccir Jan 30 '12 at 00:34
0

How about this awesomeness... (not really)

// .h
int aTiles[10][2];

// .m
int a[10][2] = {
        { 0,  0}, // 0
        { 0,  1}, // 1
        { 1,  5}, // 2
        { 0,  0}, // 3
        { 0,  0}, // 4
        { 0,  0}, // 5
};

for (int r = 0; r <= 5; r++) {
   for (int c = 0; c < 2; c++) {
      aTiles[r][c] = a[r][c];
   }
}

definitely a work around, but... cant figure out any other way. If anyone figures out the correct way, please let me know :P

GameMaker
  • 21
  • 3