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 に答える