0

I have a cocos2d game setup which uses basic inheritance setup which is similar to this;

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

All the properties live in an array listOfProperties and I am trying to print out each property in a NSLog, but I am not sure how to do it.

For example,

// 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

The problem is that some properties do not share the same attributes. For example, an Office might have "floors" but a Warehouse has "capacity".

What I'm needing is to print out all the different properties, but I am not sure how to change the focus from the pointer prop to a pointer for the specific class (ie: Office).

I need them all to live in listOfProperties so that I can use it later on in a CCMenu and want to avoid splitting them up into seperate arrays which will be very hard for me to manage.

Is there a way to do this?

Thanks

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 回答