2

i want to initialize a 2d array.. but my code doesn`t work,

can somebody tell me what is wrong?

Thanks Chris

@interface Map : NSObject {
    int mapData[8][8];  
}
@property(readwrite) int** mapData;
@end


@implementation Map
@synthesize **mapData; (Error: Syntax before *)

- (id)initWithMap:(int[8][8])map {

    for (int i=0; i<8; i++) {
        for (int j=0; j<8; j++) {
            self.mapData[i][j] = map[i][j];
        }
    }
    return self;
}

@end

(Warning mapData requires method... use @synthesize or.......)

Edit: if i remove the type at synthesize as suggested, the compiler tells me another error:error type of property mapData does not match type of ivar mapData

Edit#2: can somebody please post the corrected code? i´m working at this really silly problem for over an hour.. (no c/c++ background, but java)

3 Answers 3

4

also

int mapData[8][8];

and

int **mapData;

is interpreted differently. The first will be an array with 64 consecutive ints and the other a pointer to a pointer to an int.

Maybe this can work for you, wrapping the 2d array in a struct...

struct map_s {
  int map[8][8];
};
typedef struct map_s map_t;

@interface Map : NSObject {
  map_t mapData;
}
@property (nonatomic, readwrite) map_t mapData;
@end


@implementation Map
@synthesize mapData;

- (id)initWithMap:(map_t)map {
  int i, j;
  for (i=0; i<8; i++) {
    for (j=0; j<8; j++) {
      self.mapData.map[i][j] = map.map[i][j];
    }
  }
  return self;
}

@end

Rewritten a little to show a map initializer

struct map_s {
  int map[8][8];
};
typedef struct map_s map_t;

@interface Map : NSObject {
  map_t mapData;
}
@property (nonatomic, readwrite) map_t mapData;
- (void)init;
- (id)initWithMap:(map_t)map;
@end


@implementation Map
@synthesize mapData;

- (void)init
{
  map_t first = {
    {
      { 0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0 },
      { 0,0,0,0,0,0,0,0 }
    }
  };
  [self initWithMap:first];
}

- (id)initWithMap:(map_t)map {
  mapData = map;
  return self;
}

@end
Sign up to request clarification or add additional context in comments.

3 Comments

i think i want the first thing.. not the pointer vector.. what do i have to do to change id?
and how would i define a map_t int the code? map_t first = { {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0} } throws hundrets of errors :-(
got it working now, i think it was some stupid syntax error :-). thanks a lot for your help! have a nice day :-)
2

I don't think you can have properties of array types. Can you just use a getter/setter instead? For example:

@interface Map : NSObject {
    int mapData[8][8];  
}
- (int)getI:(int)i j:(int)j;
- (int)setI:(int)i j:(int)j to:(int)v;
@end


@implementation Map

- (id)initWithMap:(int[8][8])map {

    for (int i=0; i<8; i++) {
        for (int j=0; j<8; j++) {
            mapData[i][j] = map[i][j];
        }
    }
    return self;
}

- (int)getI:(int)i j:(int)j {
    return mapData[i][j];
}

- (void)setI:(int)j j:(int)j toValue:(int)v {
    mapData[i][j] = v;
}

@end

Comments

0

The synthesize statement of a property does not need to specify the type, just the name.

So;

@synthesize mapData;

Also because mapData is an instance variable you do not need to use the "self.mapData" syntax. You could just do;

mapData[i][j] = map[i][j];

Finally, realise that these are just blocks of data so you can copy everything in one go. E.g

// could also be sizeof(mapData) but this is more instructive
memcpy(mapData, map, sizeof(int) * 8 * 8); 

1 Comment

okay, i now removed they type information.. now i get the error: error type of property mapData does not match type of ivar mapData

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.