声明结构的@class等价物

时间:2014-03-25 15:16:45

标签: objective-c

我想避免在标题中导入AVFoundation框架,但我需要声明CMTime

对于NSObject,我可以执行@class AVPlayer;并导入.m文件中的所有内容。如何使用像CMTime

这样的结构来完成此操作

1 个答案:

答案 0 :(得分:5)

如果你需要在标题中引用CMTime结构,你需要包括<CMTime.h>:正向声明一个struct允许你在指针声明中使用类型的名称,但不是该结构类型成员的声明。

换句话说,你可以这样做:

struct CMTime; // Forward declaration of the struct

@interface MyInterface : NSObject
-(void)fillCmTime:(CMTime*)buffer;
@end

但你不能这样做:

struct CMTime; // Forward declaration of the struct
@interface MyInterface : NSObject {
    // This is not allowed
    CMTime time;
}
// This is not allowed either
-(CMTime)getTime;
@end

您可以执行@class AVPlayer然后在成员声明中使用它的原因是Objective-C类(id - 类型)被实现为指针。实际上,如果没有星号,则无法声明id - 类型的变量。

相关问题