I am quite confused on how to access variables across files.
For example:
I have 3 files: Apple, Fruit, and Eat
Fruit.h
@interface Fruit
{
NSString *name;
}
@property (nonatomic, retain) NSString *name;
@end
Fruit.m
@implementation Fruit
#import "Fruit.h"
{
@synthesize name;
-(id) init
{
self = [super init];
if (self) {
name = [[NSMutableArray alloc] init];
}
return self;
}
}
@end
Apple.h
@interface Apple
#import Fruit.h
{
Fruit *apple;
}
@property (nonatomic, retain) Fruit *apple;
@end
Apple.m
#import Apple.h
@implementation Apple
@synthesize apple;
apple = [[Fruit alloc] init];
apple.name = @"apple";
@end
//my Eat.h is practically empty because I don't think I need it
Eat.m
@implementation Eat
#import Apple.h
//why is this not working?
NSLog(@"I am eating %@", apple.name);
I wrote these just as examples from scratch. So ignore silly syntax errors such as missing semi-colons, and obvious things I missed. I'm just mirroring something I am struggling with.
I guess my confusion is that in Apple.m, you can access the Fruit's name ivar with the period symbol (.). But in Eat.m, I cannot access apple's name ivar with a (.). I know I should/could write a getter method, but is there a way to directly access variables in the way I am trying to across files? I know its probably bad programming technique (if it can even be done), but I am just confused why the functionality isn't the same.