-1

Here is an example constructor in Java:

public Board(int row, int column)
{
    this.row = row;
    this.column = column;
}

...

int row;
int column;

and here is my method in Objective C I am trying to do the same thing:

- (void) setSquares: (int) row:(int) column
{
    self.row = row; // <-- Error
    self.column = column;// <-- Error
}

...

int row;
int column;

As you can see I get 2 errors because the compiler thinks I am trying to access 2 properties, one called row and one called column. I know this is how you are suppose to access properties but how are you suppose to 'change the scope' so that I can set the local variables to the parameters of the method? How do I do this in Objective C?

John
  • 3,769
  • 6
  • 30
  • 49
  • You need a property to do that. – Justin Meiners Oct 25 '12 at 21:15
  • 2
    I have to admit, I always have found it poor practice to use a parameter name the same as an internal object variable name. – trumpetlicks Oct 25 '12 at 21:17
  • 1
    My two cents: **do fully read** an Objective-C language reference. You don't even know the correct syntax. [Read this as well.](http://stackoverflow.com/questions/683211/method-syntax-in-objective-c) –  Oct 25 '12 at 21:23

3 Answers3

1

Your routine in Objective C is written incorrectly.

It should be:

-(void)setSquares:(int)row col:(int)column{
    self.row = row;
    self.column = column;
}
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
1

That Java constructor would generally be translated like this:

@interface Board : NSObject

@property (nonatomic, assign) int row;
@property (nonatomic, assign) int column;

@end

@implementation Board

- (id)initWithRow:(int)row andColumn:(int)column {
    if (self = [super init]) {
        self.row = row;
        self.column = column;
    }
    return self;
}

@end
mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
1

Just rename the metod paramethers:

- (void)setSquares:(int)newRow col:(int)newColumn
{
    row = newRow;
    column = newColumn;
}
Bartosz Ciechanowski
  • 10,293
  • 5
  • 45
  • 60
  • Neither is this the problem. What you think is not allowed is in fact allowed (bad practice, however), but OP fails to understand the concept of labels and their connection to method names. –  Oct 25 '12 at 21:24