Question

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.

Était-ce utile?

La solution

Try to use the @property instead of int _across; and int _down;. and remove the sets method, because if you use the @property, the initializer are automatically provided by Xcode.

so try :

@property (nonatomic) int across;

@property (nonatomic) int down;

and remove int _across; and int _down. If you need more information about properties, and how does they work: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

Autres conseils

Many thanks to Max_Power89. His answer is correct. Finished code now looks like :

.h File

//
//  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

@property (nonatomic) int across;
@property (nonatomic) int down;

-(id)init;
-(id)initWithPos :(int)newAcross :(int)newDown ;

-(void)set:(int)newAcross :(int)newDown;
-(void)showResults;

@end

.m File

//
//  tttMove.m
//  TicTocToe
//
//  Created by Matthew Baker on 12/09/2013.
//  Copyright (c) 2013 Matthew Baker. All rights reserved.
//

#import "tttMove.h"

@implementation tttMove

@synthesize across;
@synthesize down;

-(id) init {
    if( (self=[super init] )) {
        [self setAcross:0];
        [self setDown:0];
    }
    return self;
}
-(id)initWithPos :(int)newAcross :(int)newDown {
    if( (self=[super init] )) {
        [self setAcross:newAcross];
        [self setDown:newDown];
    }
    return self;
}
-(void)showResults {

    NSLog(@"move position %i,%i",[self across],[self down]);
}

-(void)set:(int)newAcross :(int)newDown {
    [self setAcross:newAcross];
    [self setDown:newDown];
}

@end

Thanks again for your help.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top