赋值后返回null的类的NSString?

时间:2012-08-20 17:37:22

标签: objective-c xcode class nsstring

我在为字符串分配值然后将其取回时遇到问题。字符串是类的属性,因为我需要它来转移到其他视图控制器。 (我已经尝试过Core Data,但它也没有用,这看起来更简单。)

字符串的类是:

.h    
@interface GameInformation : NSObject

@property (nonatomic, retain) NSString *word;
@property (nonatomic, retain) NSString *mode;

@end

.m
@implementation GameInformation

@synthesize mode = _mode;
@synthesize word = _word;

@end

非常简单。 app delegate中的相关代码:

GameInformation *gameInfo = [GameInformation alloc].init;
optionsView.gameInfo = gameInfo;
oneBox.gameInfo = gameInfo;
twoBox.gameInfo = gameInfo;

等等,直到我覆盖了所有控制器。选项视图是我设置字符串值的地方,并测试这些值。

GameInformation *info = [self gameInfo];
UISegmentedControl *selectMode = [self mode];
UISegmentedControl *gameWord = [self word];
if (selectMode.selectedSegmentIndex == 0)
{
    info.mode = @"regular";
} else if (selectMode.selectedSegmentIndex == 1) {
    info.mode = @"wordTap";
}
if (gameWord.selectedSegmentIndex == 0)
{
    info.word = @"regular";
} else if (gameWord.selectedSegmentIndex == 1) {
    info.word = @"hat";
} else if (gameWord.selectedSegmentIndex == 2) {
    info.word = @"dog";
} else if (gameWord.selectedSegmentIndex == 3) {
    info.word = @"book";
} else if (gameWord.selectedSegmentIndex == 4) {
    info.word = @"bus";
} else if (gameWord.selectedSegmentIndex == 5) {
    info.word = @"cup";
} else if (gameWord.selectedSegmentIndex == 6) {
    info.word = @"pig";
}

NSLog(@"%@", info.mode);
NSLog(@"%@", info.word);

当这些日志被传递时,日志会显示为null。我试过了[info setWord:@“regular”];和[info setMode @“regular”];但那些也不起作用。

我还没有尝试在一个盒子,两个盒子等控制器中使用字符串,因为测试返回null。

因此。我究竟做错了什么?还是我在错误的树上吠叫?并且,正如我之前所说,尝试使用核心数据并不起作用,这似乎是一种更简单的方法。

提前致谢!

编辑: 谢谢大家的快速评论!我在信息上做了一个NSLog,它确实是空的。我还将声明更改为复制而不是保留,并更改了alloc语句中的点表示法。 alloc语句位于app delegate的didFinsihLaunchingWithOptions中。这是错误的地方吗?

再次感谢您的帮助!

0 个答案:

没有答案