开发者

Basic Objective C Problem

开发者 https://www.devze.com 2023-03-04 20:50 出处:网络
In my driver class I call this method: NSMutableArray *test = [[myGrid getLoc:5 : 5] getAdjacentLocation: 2 : 2];

In my driver class I call this method:

NSMutableArray *test = [[myGrid getLoc:5 : 5] getAdjacentLocation: 2 : 2];

The header and the first line of the implementation of getAdjacentLocation is:

- (NSMutableArray*) getAdjacentLocation: (int)w: (int)h{
    NSLog(@"checkpoint");
}

The implementation of getLoc is:

- (Location*)getLoc:(int)i : (int)j {
    NSLog(@"check");
    return (Location*)locations[i][j];
}

This is suppose to return a location out of an 2d array. Although the NSLog is call, I dont think it returns it properly? (can I get 2d arrays with properties?)

I init locations like this

int x = 0; int y = 0;

        //row
        for(int i = 0; i < 1000; i+=40){
            //column
            for(int j = 0; j < 1000;开发者_运维百科 j+=40) {

                locations[y][x] = [[Location alloc] initWithPosition:CGPointMake(i, j)];

                NSLog(@"%@" ,NSStringFromCGPoint(locations[y][x].locPosition));

                y++;


            }

            x=0;
            y++;

        }

I could be doing something stupid with memory management as this is completely new to me.

The first statement in this method of getadjacentlocation is not executed. Why? This is probably a very stupid mistake, but can anyone help me out here? Thanks.


Your getLoc:: method is probably returning nil, because in your locations initialization code, you're only incrementing y, not x, leaving most of the elements of location undefined. Fix your loop to increment x inside the column for loop.


Obj-c methods are supposed to have parameters and names. You ought to name your parameters, so your method should be declared getAdjacentLocationWidth:(int)w height:(int)h and called like [object getAdjacentLocationWidth:2 height:2];. I added the width and height words to the method name so you can tell which parameter is which.

(You should also go and name the parameters in the -getLoc:: method, too.)

Note: edited down from an answer that was an incorrect answer to a recommendation of named parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消