Hi here is a code:
Code:
#import <Foundation/Foundation.h>
// --------------------------------------------------
@interface Tire : NSObject
@end // Tire
@implementation Tire
- (NSString *) description
{
return (@"I am a tire. I last a while");
} // description
@end // Tire
// --------------------------------------------------
@interface Engine : NSObject
@end // Engine
@implementation Engine
- (NSString *) description
{
return (@"I am an engine. Vrooom!");
} // description*/
@end // Engine
// --------------------------------------------------
@interface Car : NSObject
{
Engine *engine;
Tire *tires[4];
int d;
}
@property int d;
- (void) print;
@end // Car
@implementation Car
@synthesize d;
- (NSString *) description
{
return [NSString stringWithFormat:@"ID: %d",d];
}
- (id) init
{
if (self = [super init]) {
engine = [Engine new];
tires[0] = [Tire new];
tires[1] = [Tire new];
tires[2] = [Tire new];
tires[3] = [Tire new];
}
return (self);
} // init
- (void) print
{
NSLog (@"%@", engine);
NSLog (@"%@", tires[0]);
NSLog (@"%@", tires[1]);
NSLog (@"%@", tires[2]);
NSLog (@"%@", tires[3]);
} // print
@end // Car
// --------------------------------------------------
int main (int argc, const char * argv[])
{
Car *car; // car.d works
//id car; // car.d doesn't work..why?
car = [Car new];
car.d=5;// ????
NSLog(@"car nalezy do: %@",[car class]);
[car print];
NSLog(@"%@",car);//
return (0);
} // main
and question: why if I use [Car*car] (in main) it differs from using id car? when I use 'id car' and disable car.d=5 it works proper (why?) I check class by using [method class] and for 'id car' and Car*car it returns Car class...