0

これに似た基本的な継承設定を使用するcocos2dゲーム設定があります。

> Property (Base class)
> - Office : Property
> - Warehouse : Property
> - Bank : Property

すべてのプロパティは配列listOfPropertiesに存在し、NSLogの各プロパティを出力しようとしていますが、その方法がわかりません。

例えば、

// Create an office
Office *o = [Office alloc] init];
[o setName:@"Some office"];
[city.listOfProperties addObject:o];
[o release];

// Debug output all the properties in the game
// city.listOfProperties in an array of Property objects
for (Property *prop in city.listOfProperties) {

    // I want to print out all the different properties here
     if ([prop isKindOfClass:[Office class]]==YES)
     {
         NSLog(@"Office");
         NSLog(@"office.name = %@", prop.name); // Prop name does not work 
     }
} // next

問題は、一部のプロパティが同じ属性を共有していないことです。たとえば、Officeには「フロア」があり、Warehouseには「容量」があるとします。

私が必要としているのは、すべての異なるプロパティを印刷することですが、フォーカスをポインターpropから特定のクラス(つまり、Office)のポインターに変更する方法がわかりません。

listOfProperties後でCCMenuで使用できるように、それらすべてが存在する必要があります。また、それらを個別の配列に分割することは避けたいので、管理が非常に困難になります。

これを行う方法はありますか?

ありがとう

4

1 に答える 1

1

このように型キャストを行います。

for (Property *prop in city.listOfProperties) {

 if ([prop isKindOfClass:[Office class]])
 {
     Office* officeObject = (Office*) prop;

     NSLog(@"office.name = %@", officeObject.name);
 }
 if ([prop isKindOfClass:[Warehouse class]])
 {
     Warehouse* WarehouseObject = (Warehouse*) prop;

     NSLog(@"Warehouse.name = %@", WarehouseObject.name);
 }
 if ([prop isKindOfClass:[Bank class]])
 {
     Bank* BankObject = (Bank*) prop;

     NSLog(@"Bank.name = %@", BankObject.name);
 }

}

ログに記録したい変数は何でも持つことができます。たとえば名前を使用します。

于 2012-10-06T09:58:56.327 に答える