NS::Array of MTL::Device objects question

Hi all. I'm trying to get C++ code working with Metal.

I get the array of MTL:Device by calling

NS::Array *device_array = MTL::CopyAllDevices();

Next, I want to get the only element of the MTL::Device array by calling

MTL::Device *device = device_array->object(0);

I get an error:

Cannot initialize a variable of type 'MTL::Device *' with an rvalue of type 'NS::Object *'

Question: how to get an MTL::Device object from NS::Array?

Hi squirtgt,

This is expected. The NS::Array::object() method returns an NS::Object*, which cannot be implicitly cast to MTL::Device*.

To solve this problem you'll need to cast the result of the expression to the appropriate type. Here's an example using static_cast:

MTL::Device* device = static_cast<MTL::Device*>(device_array->object(0));

After this, you can access the functionality of the Metal device through the device pointer.

NS::Array of MTL::Device objects question
 
 
Q