Obj-C: Unknown type name CHHapticPatternPlayer

I'm doing this:


@implementation RootViewController
static CHHapticEngine *hapticEngine API_AVAILABLE(ios(13.0));
static CHHapticPatternPlayer *hapticPlayer API_AVAILABLE(ios(13.0));


But Xcode reports an error on the third line: "Unknown type name CHHapticPatternPlayer, did you mean CHHapticPatternKey?"


Further in my code I have this line:


hapticPlayer = [hapticEngine createPlayerWithPattern:pattern error:NULL];


And there Xcode says this error:

"Assigning to 'CHHapticPatternKey *' (aka 'NSString **') from incompatible type 'id<CHHapticPatternPlayer> _Nullable'"

Of course the pattern was first successfully initialized (code omitted here for clarity).


Might this issue be a bug in the framework?

Accepted Reply

Hi fcmapps, thanks for writing.


First, why are you including the API_AVAILABLE tags with your variable declarations? I don't think you can override what is declared in the public headers. You should just have:


static CHHapticEngine *hapticEngine;


Perhaps that does not matter for this issue -- your compile problem is due to the fact that the declaration of createPlayerWithPattern:error is the following:


- (nullable id<CHHapticPatternPlayer>)createPlayerWithPattern:(CHHapticPattern *)pattern error:(NSError **)outError;


Note that the player's type is id<CHHapticPatternPlayer>, so:


static id<CHHapticPatternPlayer> hapticPlayer;


Note, no "*" because this is not a pointer to a @class but rather a @protocol


Please consult here for reference: https://developer.apple.com/documentation/corehaptics?language=objc


-DS

Replies

Hi fcmapps, thanks for writing.


First, why are you including the API_AVAILABLE tags with your variable declarations? I don't think you can override what is declared in the public headers. You should just have:


static CHHapticEngine *hapticEngine;


Perhaps that does not matter for this issue -- your compile problem is due to the fact that the declaration of createPlayerWithPattern:error is the following:


- (nullable id<CHHapticPatternPlayer>)createPlayerWithPattern:(CHHapticPattern *)pattern error:(NSError **)outError;


Note that the player's type is id<CHHapticPatternPlayer>, so:


static id<CHHapticPatternPlayer> hapticPlayer;


Note, no "*" because this is not a pointer to a @class but rather a @protocol


Please consult here for reference: https://developer.apple.com/documentation/corehaptics?language=objc


-DS

Thank you, that solved my issue.

The reason I added the API_AVAILABLE tags was to keep Xcode from showing warnings all the time (my app's target is iOS 9 and higher).