Objective-C Reference to object that implements protocol

74 views Asked by At

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?

1

There are 1 answers

0
newacct On BEST ANSWER

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.