Same Class Object With Different Methods For iOS and watchOS (Objective-C)

问题描述:

I am currently working on an iOS app that includes a watchOS app extension. In my iOS app I've made an NSObject subclass, which makes use of some objects from the AVFoundation framework in some of its methods.

On the Watch Extension side, I'm creating an NSDictionary object and then sending it to my iOS counterpart using the WatchConnectivity framework. I then use the dictionary to initialize my custom class.

I would like to be able to add my Watch Extension as one of the Targets for my custom class's .m file so that I can go ahead and create objects of my custom class on the watch and then send it over. (My class conforms to the NSCoding protocol so I can use NSKeyArchiver to represent my object using NSData, add it to a dictionary, then transferring that dictionary to the iOS device).

The Problem: AVFoundation is not available in watchOS, which my class utilizes. So is there a way to "omit" certain methods of a class depending which OS it's in.

Ex: In iOS, UIImage has the method initWithCIImage: but it is not available in watchOS since the CoreImage framework is not available.

(Perhaps I should recreate the class specifically for watchOS with all the same instance variables and use the same keys for the classes in the NSCoding methods, omitting AVFoundation from the watchOS version?)

Any help would be greatly appreciated!

网友答案:

You can build your class using the macros available in TargetConditionals.h, for example TARGET_OS_IOS or TARGET_OS_WATCH, e.g.: