I wanna do pass by reference in ObjC.
Here is the illustration of the scenario:
// MainClass.h
@interface MainClass
{
Board *board;
OtherClass *other;
}
@end
// MainClass.m
@implementation MainClass
- (id)init
{
if (self = [super init])
{
board = [[Board alloc] init];
other = [[OtherClass alloc] initOtherClassWithBoard: board];
}
return self
}
@end
// Other class.h
@interface OtherClass
{
Board *refBoard;
}
- (void)initOtherClassWithBoard: (Board*)ref;
@end
@implementation OtherClass
- (id)initOtherClassWithBoard: (Board*)ref
{
if (self = [super init])
{
refBoard = ref;
}
return self;
}
@end
So my question is does my OtherClass is holding a reference Board from MainClass?
Or OtherClass’s Board is a copy but not a reference.
If I wanna do a reference then how should I change it?
EDIT:
What if I add this on both classes
@property (nonatomic, retain) Board *board;
You always refer to objects in Objective-C via pointers, a.k.a. references. Therefore, objects are always passed by reference in Objective-C.