New to XCode, coming from a VB.net background so may be missing something basic.
Have created a .h and .m file for a basic class. Code listed below.
//
// tttMove.h
// TicTocToe
//
// Created by Matthew Baker on 12/09/2013.
// Copyright (c) 2013 Matthew Baker. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface tttMove : NSObject
-(id)init;
-(id)initWithPos :(int)newAcross :(int)newDown ;
-(void)setAcross:(int)newAcross;
-(void)setDown:(int)newDown;
-(void)set:(int)newAcross :(int)newDown;
-(int)across;
-(int)down;
-(void)showResults;
@end
//
// tttMove.m
// TicTocToe
//
// Created by Matthew Baker on 12/09/2013.
// Copyright (c) 2013 Matthew Baker. All rights reserved.
//
#import "tttMove.h"
@implementation tttMove
int _across;
int _down;
-(id) init {
if( (self=[super init] )) {
_across = 0;
_down = 0;
}
return self;
}
-(id)initWithPos :(int)newAcross :(int)newDown {
if( (self=[super init] )) {
_across = newAcross;
_down = newDown;
}
return self;
}
-(void)showResults {
NSLog(@"move position %i,%i",_across,_down);
}
-(void)setAcross:(int)newAcross {
_across = newAcross;
}
-(void)setDown:(int)newDown {
_down = newDown;
}
-(void)set:(int)newAcross :(int)newDown {
_across = newAcross;
_down = newDown;
}
-(int)across {
return _across;
}
-(int)down {
return _down;
}
@end
Problem I'm having is that I when I create multiple instances of the same class, they always share common values. Cannot update one without the other...
-(void) test {
tttMove *move1 = [[tttMove alloc] initWithPos:1 :1];
[move1 showResults];
tttMove *move2 = [[tttMove alloc] initWithPos:2 :2];
[move2 showResults];
[move1 showResults];
}
The output I get is:
2013-09-15 23:01:35.004 TicTocToe[19925:c07] move position 1,1
2013-09-15 23:01:35.006 TicTocToe[19925:c07] move position 2,2
2013-09-15 23:01:35.006 TicTocToe[19925:c07] move position 2,2
Which means although I'm calling the alloc init, I'm not getting a new instance.
I'm assuming I've missed something basic, but googling hasn't helped. Probably not even searching for the right thing.
Thanks in advance for the help guys.