I want to create a reference to an object that implements a protocol. This is an example code:
@interface TestClass ()
@property id <TestProtocol> testProperty;
@end
@implementation TestClass
+(id)init :(id <TestProtocol> test)
{
TestClass *testClass = [[TestClass alloc] init];
testClass.testProperty = test;
return testClass;
}
@end
Does it then create a copy of the test object that implements the protocol or a reference? I'm wondering since there is no "*" sign. If it is a copy how do I then create a reference instead?
The type
id
is a pointer type that can hold a pointer to any object.There is no type in Objective-C that holds an "object" itself. Objects can only be used through pointers to objects.