在 Swift 中使用 obj-c typedef [英] Using obj-c typedef in Swift
问题描述
我有一个 typedef:
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;
我已将此文件包含在桥接头中.但是,当我尝试在 Swift 文件中访问 VMVideoCategoryType
时出现错误:
I've included this file in the bridging header. However, when I try to access VMVideoCategoryType
in a Swift file I get an error:
使用未声明的类型VMVideoCategoryType"
有什么办法可以使这项工作正常进行,还是我必须在 Swift 中完全重新定义这种类型?
Is there any way to make this work or do I have to completely re-define this type in Swift?
推荐答案
我有点猜测,不过原因好像是 Objective-C不能静态分配像 NSString
这样的对象(参见例如 Objective-C 中的对象是否曾经在堆栈上创建?).如果
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;
被导入 Swift 然后你可以声明一个局部变量
were imported into Swift then you could declare a local variable
var foo : VMVideoCategoryType
这将是一个 NSString
而不是一个指向 NSString
的指针.
which would be a NSString
and not a pointer to NSString
.
还要注意,你在 Swift 中看到的 NSString
对应于 NSString *
在 Objective-C 中.
Note also that what you see in Swift as NSString
corresponds to NSString *
in Objective-C.
如果您将 VMVideoCategoryType
定义为 NSString *
的 typedef然后它在 Swift 中可见:
If you define VMVideoCategoryType
as a typedef for NSString *
then it is visible in Swift:
typedef NSString * VMVideoCategoryType;
extern VMVideoCategoryType const VMVideoCategoryType_MusicVideo;
// ...
这篇关于在 Swift 中使用 obj-c typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!