1 Reply
      Latest reply on Jun 30, 2019 3:30 PM by macuser1984
      macuser1984 Level 1 Level 1 (0 points)

        I've got a .h file set up just like in the Metal Xcode template (I copy and pasted the relevant content from one of those files into another existing project not made from the template), but when I define a struct in there similar to how it's defined in the template, my Swift code isn't finding the definition - so the compiler says the struct type doesn't exist. The shaders seem to be fine since I've got the #include statement for the shared .h file in the shader file, so it's just the Swift code that seems to have an issue. Any suggestions on how to fix this? Below is a sample of what I tried.

         

        #ifndef ShaderTypes_h
        #define ShaderTypes_h
        
        
        #ifdef __METAL_VERSION__
        #define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
        #define NSInteger metal::int32_t
        #else
        #import <Foundation/Foundation.h>
        #endif
        
        #include <simd/simd.h>
        
        typedef struct {
            int test;
        } Test;
        
        #endif /* ShaderTypes_h */
        

         

        In the Swift code I just tried defining an instance of the struct type, and then I get use of undefined type/identifier errors.

        • Re: Defining a struct in a .h file for access in shaders and Swift code
          macuser1984 Level 1 Level 1 (0 points)

          In looking at this further, I realized that somehow a bridging header didn't get selected/added when I added this file into my Xcode project (which is otherwise all Swift code). If anyone else runs into a similar situation (but I think Xcode should prompt to add a bridging header when you add a .h file to an all-Swift project), I fixed this issue by going into the build settings in the Swift Compiler - General section and filling in the Objective-C bridging header value (which was previously empty) with the shader types .h filename (which is how it's set up in the Xcode Metal template also). So if your target is named MyApp, and your shader types file is named ShaderTypes.h, then the value in Objective-C bridging header would be MyApp/ShaderTypes.h. This assumes that the single shader types file is the only .h file in your project you need exposed to Swift - adjust accordingly if you need to expose multiple .h files to Swift.