无论我做什么,我似乎无法初始化任何这些属性,我总是得到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 :(得分: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)
}