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)
}
4

1 回答 1

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)
}
于 2013-04-03T19:55:52.437 回答