10

I have a typedef as so:

typedef NSString VMVideoCategoryType;

extern VMVideoCategoryType *const VMVideoCategoryType_MusicVideo;
extern VMVideoCategoryType *const VMVideoCategoryType_Audio;
extern VMVideoCategoryType *const VMVideoCategoryType_Performance;
extern VMVideoCategoryType *const VMVideoCategoryType_Lyric;
extern VMVideoCategoryType *const VMVideoCategoryType_Show;

I've included this file in the bridging header. However, when I try to access VMVideoCategoryType in a Swift file I get an error:

Use of undeclared type 'VMVideoCategoryType'

Is there any way to make this work or do I have to completely re-define this type in Swift?

zneak
  • 134,922
  • 42
  • 253
  • 328
OdieO
  • 6,836
  • 7
  • 56
  • 88

1 Answers1

16

I am a little bit speculating, but the reason seems to be that Objective-C objects like NSString cannot be statically allocated (see e.g. Are objects in Objective-C ever created on the stack?). If

typedef NSString VMVideoCategoryType;

were imported into Swift then you could declare a local variable

var foo : VMVideoCategoryType

which would be a NSString and not a pointer to NSString.

Note also that what you see in Swift as NSString corresponds to NSString * in Objective-C.

If you define VMVideoCategoryType as a typedef for NSString * then it is visible in Swift:

typedef NSString * VMVideoCategoryType;

extern VMVideoCategoryType const VMVideoCategoryType_MusicVideo;
// ...
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • This works for getting rid of Swift errors but then my previous type declarations in Obj-C using that type throw errors: `Implicit conversion of an indirect pointer to an Objective-C pointer to 'NSString *' is disallowed with ARC` – OdieO Jun 29 '15 at 22:34
  • @Ramsel: You have to replace `VMVideoCategoryType *` by `VMVideoCategoryType` after that change (as I did in the extern definitions). – Martin R Jun 29 '15 at 22:51
  • @Ramsel: If that does not help, please add the Objective-C code causing the problem to your question. – Martin R Jun 30 '15 at 06:47
  • So in the end I didn't want to change the obj-c code that referenced the typedef, and I made a compromise to not refer to the type in Swift. I just reference it as a String. I am able to refer to the secondary constants of that type since they're pointers... This is an isolated case where we typedef'd an NSString "enum/struct" in Obj-c, so the isolated fix works for now. – OdieO Jul 02 '15 at 17:27