I’m writing a media player framework for a project I’m working on. This depends on VLC. One of my classes’ header file looks like this
#import <vlc/vlc.h>
@interface MediaPlayerVLC : MediaPlayer
{
libvlc_media_player_t *player;
libvlc_media_t *media;
}
@end
I need the instance variables in the class, and I need the #import <vlc/vlc.h>, because they’re defined in there. But I don’t want users of this framework to have to import all of VLC’s headers just for these two types. I’ve seen a few solutions to this problem around…
- Forward declaration, such as
@class. Unfortunately, these types aretypedef structtypes, I can’t seem to find any way to forward declare them - declare the ivars as
void *, then cast them whenever I want to use them. I’d like to avoid this if possible, as we lose type-safety and implementation files become full of ugly casts. -
I’ve seen this in Apple’s frameworks…
@interface CAAnimation : NSObject <NSCoding, NSCopying, CAMediaTiming, CAAction> { @private void *_attr; uint32_t _flags; }What does
_attrpoint to? I guess it would be a struct of ivars, I’m curious what advantages this has… - Two header files for the class, one public and one private. The private one would look like the above, and the public would just have
void *pointers. This is pretty ugly, as I’d have to be very careful to keep them both in sync.
What’s considered best practise? Is there an approach I’ve missed?
You can use class extensions. You should try doing this:
MediaPlayerVLC.h:
MediaPlayerVLC.m:
From Apple’s docs:
That’s using a class extension category to declare extra ivars in the implementation file.