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,但我没有得到一个新实例。
我假设我错过了一些基本的东西,但谷歌搜索并没有帮助。可能甚至没有寻找正确的东西。
提前感谢帮助大家。
尝试使用 @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
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
再次感谢您的帮助。