Game クラスがあるとします。
@interface Game : NSObject
CutthroatGame サブクラスで:
@interface CutthroatGame : Game
ViewController .h ファイルに次のようなプロパティがあるとします。
@property (strong) Game *game;
ViewController .m ファイルで次のようにプロパティを安全にオーバーライドできますか。
if (_playerCount == 3) {
_game = [[CutthroatGame alloc] init];
else {
_game = [[Game alloc] init];
}
編集:これが機能するはずの場合、以下のエラーにどのように対処すればよいですか?
CutthroatGame は、次のような多くの追加プロパティを定義します。
@property (strong) Player *opponent2
ViewController の _game プロパティを使用してそれらにアクセスしようとすると、次のエラーが発生します。
_game.opponent2 = [players objectAtIndex:0]; -- ERROR: Property 'opponent2' was not found on object of type 'Game *'