0

XCode 的新手,来自 VB.net 背景,所以可能缺少一些基本的东西。

为基本类创建了一个 .h 和 .m 文件。下面列出的代码。

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

我遇到的问题是,当我创建同一个类的多个实例时,它们总是共享共同的值。没有另一个就无法更新...

-(void) test {

    tttMove *move1 = [[tttMove alloc] initWithPos:1 :1];
    [move1 showResults];

    tttMove *move2 = [[tttMove alloc] initWithPos:2 :2];
    [move2 showResults];

    [move1 showResults];

}

我得到的输出是:

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

这意味着虽然我调用了 alloc init,但我没有得到一个新实例。

我假设我错过了一些基本的东西,但谷歌搜索没有帮助。可能甚至没有寻找正确的东西。

在此先感谢您的帮助。

4

2 回答 2

1

尝试使用 @property 而不是int _across; 和int _down;。并删除 sets 方法,因为如果您使用 @property,初始化程序将由 Xcode 自动提供。

所以试试:

@property (nonatomic) int across;

@property (nonatomic) int down;

并删除int _across;和int _down。如果您需要有关属性及其工作原理的更多信息: https ://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

于 2013-09-15T22:35:42.577 回答
0

非常感谢 Max_Power89。他的回答是正确的。完成的代码现在看起来像:

.h 文件

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

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

再次感谢您的帮助。

于 2013-09-16T11:26:42.717 回答