行。我有一个“游戏”类,它创建了我的“棋盘”类的一个实例,并对其进行了测试。
“Board”类有一个字典,它似乎无法保持它的值(?)试图将代码限制到最小:
游戏类:
@interface Game : UIViewController{
Board *board;
}
-(void)testAgain;
@implementation Game
-(void)setup{
board = [Board alloc]createBoard];
[board test]; //returns right value
[self testAgain]; //returns (null), see output below
}
-(void)testAgain{
[board test];
}
-(void)didLoad{
[self setup];
}
板级:
@interface Board : NSObject{
@property(nonatomic, retain) NSMutableDictionary *dict;
-(Board *)createBoard;
-(void)test;
@implementation Board
@synthesize dict;
-(Board *)createBoard{
dict = [[NSMutableDictionary alloc]init];
[dict setObject:@"foo1" forKey:@"1"];
[dict setObject:@"foo2" forKey:@"2"];
[dict setObject:@"foo3" forKey:@"3"];
[dict setObject:@"foo4" forKey:@"4"];
[dict setObject:@"foo5" forKey:@"5"];
return self;
}
-(void)test{
NSLog(@"Test return: %@", [dict objectForKey:@"4"]);
}
以下输出:
2012-06-23 01:05:28.614 Game[21430:207] Test return: foo4
2012-06-23 01:05:32.539 Game[21430:207] Test return: (null)
在此先感谢您的帮助!