Sign in to follow this  
nick5454

array in obj c for i phone not getting value

Recommended Posts

I creayed a class with a 2 dimensional array and I can see that the values are being set, but every time I call getMapLocation it returns 0, which is invalid because they are set to 1 Can anyone tell me what I am doing wrong? Map.h file @interface Map : NSObject { int groundMap[5][5]; int mapLevel; } @property int mapLevel; -(int) getMapLocation: (int) x : (int) y; -(void) setMapLocation: (int) y : (int) y : (int) value; @end Map.m #import "Map.h" @implementation Map @synthesize mapLevel; -(id)init { for(int y=0; y<5; y++) { for( int x=0; x<5; x++) { if( x == 0 || x == 5 || y == 0 || y == 5 ) [self setMapLocation: x : y : 1]; else [self setMapLocation: x : y : 1]; } } mapLevel = 1; return 0; } -(int) getMapLocation: (int) x : (int) y { return groundMap[x][y]; } -(void) setMapLocation: (int) x : (int) y : (int) value { groundMap[x][y] = value; } @end MyView file Map *playerMap; playerMap = [[Map alloc] init]; for( int y=0; y<5; y++) { for( int x=0; x<5; x++) { point.x = (x * 32); point.y = (y * 32); int num = 0; num =[playerMap getMapLocation: x : y]; switch( num ) { case 0: [brickTex drawAtPoint:point]; break; case 1: [sidewalkTex drawAtPoint:point]; break; default: break; } NSLog(@"Added %i", num); // always 0 } }

Share this post


Link to post
Share on other sites
First, use the [source] tag when posting large amounts of code. It preserves indentation, too, which makes code more readable.

There are a number of common "best" practices that are missing here, and one or two are potentially problematic.
  1. First, when implementing your init method, you fail to initialize the base object type and you fail to test for the validity of self. This could cause an access error should the initialization ever fail.

  2. Second, you send a message to a selector while still constructing your object. I don't know enough about Objective-C internals yet to tell you if this is safe, but if I were you I would replace the calls to setMapLocation:y:y:value with direct member variable access (groundMap[x][y] = 1;).

  3. Third, and perhaps most important, you named both index parameters in the setMapLocation:y:y:value the same - y! Remember that objects in Objective-C do not error if they do not handle a message for a given selector, so your application is likely failing silently. Correct that selector as setMapLocation:x:y:value and see if that doesn't fix your problem.

Good luck!

Share this post


Link to post
Share on other sites
Yeah I was using groundMap directly, but since it wasn't working and i'm new to objective c, I thought that maybe it expected only operations to be called.

I added

[super init]

...

return self;

and it works!

Thanks a lot.

Nick

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this