2013-04-03 149 views
0

无论我做什么,我似乎都不能初始化这些属性,我总是得到0或null作为输出。属性没有初始化

Player.h: 
@interface Player : NSObject 
{ 
    NSString *name; 
} 
@property (nonatomic, strong) NSString *name; 
@end 

Player.m: 
@implementation Player 
@synthesize name; 
@end 



MainGameDisplay.h: 
#import "Player.h" 
@interface MainGameDisplay : UIViewController<UIScrollViewDelegate> 
{ 
    Player *player, *rival1, *rival2, *rival3; 
} 

MainGameDisplay.m: 
-(void) initCharAttributes { 
    player = [[Player alloc] init]; 
    player.name = @"PlayerName"; 
    NSLog(@"NAME:%@", player.name); //Output= NAME:(null) 
} 
+0

什么是您的目标版本?你也有最新版本的XCode?如果是这样,你可以摆脱@synthesize和支持iVar,因为它们对于最新的编译器来说不是必需的。 – 2013-04-03 19:27:55

+0

@Ben M iPhone模拟器6.1 – PappaSmalls 2013-04-03 19:30:17

+0

转到您的目标并查看部署目标字段。这是我所指的目标版本。谢谢! – 2013-04-03 19:42:15

回答

1

尝试这些更改。您不需要在MainGameDisplay.h上公开如此多的实现。你的属性也会自动合成,所以你的@synthesize和支持iVar是没有必要的。另外,你不应该用init来启动一个方法名,除非它负责初始化你的类的一个实例。

Player.h: 
@interface Player : NSObject 

@property (nonatomic, strong) NSString *name; 

@end 

Player.m: 
@implementation Player 

@end 



MainGameDisplay.h: 
@interface MainGameDisplay : UIViewController 

MainGameDisplay.m: 
#import "Player.h" 

@interface MainGameDisplay() <UIScrollViewDelegate> 

@implementation MainGameDisplay { 
    Player *player, *rival1, *rival2, *rival3; 
} 

- (void)charAttributes { 
    player = [[Player alloc] init]; 
    player.name = @"PlayerName"; 
    NSLog(@"NAME:%@", player.name); //Output= NAME:(null) 
} 
+0

还没有得到最新版本的Xcode,所以我仍然需要综合。我改变了你的建议的其余部分,但它仍然无法正常工作。 – PappaSmalls 2013-04-03 20:01:17

+0

如果您能够为6.0构建,则您的XCode版本包含自动合成。确保您使用Apple LLVM编译器4.2(在编译设置上搜索编译器)。你在哪里调用charAttributes? – 2013-04-03 20:02:53

+0

其他一些方法,我只包含与名称和播放器有关的代码。 – PappaSmalls 2013-04-03 20:03:44