Search code examples
objective-cmetal

What's the difference between id<MTLDevice> and MTLDevice*?


When accessing a MTLDevice, the tutorial always encourages id<MTLDevice> rather than MTLDevice*.

If id is used as a pointer pointing to any ObjC object, then what's the difference between id<MTLDevice> and MTLDevice*?

Why MTLDevice is not correct? It shows "Type argument 'GDevice *' (aka 'struct GDevice *') is neither an Objective-C object nor a block type"


Solution

  • The syntax id<P> means “an object that conforms to protocol P”.

    In contrast, T* means “a pointer to an object of type/class T”.

    But MTLDevice is a protocol and not a type/class. This is why we use id<MTLDevice>, not MTLDevice *.

    So, consider the following:

    id<MTLDevice> device;
    

    That means that device is a reference to an object that conforms to the MTLDevice protocol.

    For more information for about the use of id with these angular brackets, see Programming with Objective-C: Working with Protocols.