Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using obj-c typedef in Swift

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?

like image 484
OdieO Avatar asked Jun 29 '15 21:06

OdieO


1 Answers

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;
// ...
like image 52
Martin R Avatar answered Nov 01 '22 20:11

Martin R