Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
639 views
in Technique[技术] by (71.8m points)

objective c - Creating an iOS/OS X cross-platform class

I read a lot of questions about creating a cross-platform library for these 2 systems. Every answer points to static library as the solution.

I don't want to end up with a static library, I'd like to create a class with methods for iOS and their counterpart for OS X.

-(void)createColor:(NSColor*);
-(void)createColor:(UIColor*);

The first problem that I have is that I can't find a way to use classes that are available only in a specific system. So for example, how could I manage a function that works with UIColor in iOS and with NSColor in OS X?

If I create a project for iOS, when I look into Foundation.h I can't find NSColor.h in the headers list.

I thought to use the TARGET_OS_MAC and TARGET_OS_IPHONE definitions to make a distinction between the two systems... I'm on the right way?

EDIT to add more info:

At the moment I have 2 targets: an iOSTestApp and OSxTestApp. For these targets I have included the needed frameworks depending on the system.

Using TARGET_OS_MAC and TARGET_OS_IPHONE works only when I select the OSXTestApp as active target. When I select the iOSAppTest, Xcode returns errors for OS X data type (i.e. NSColor)

Here an example of the code that produces these errors:

#if TARGET_OS_MAC
-(void)createColor:(NSColor*)color;    

#elif TARGET_OS_IPHONE
-(void)createColor:(UIColor*)color;

#endif

While if I invert the definitions it works ... Here an example of the code that produces these errors:

#if TARGET_OS_IPHONE
-(void)createColor:(UIColor*)color;

#elif TARGET_OS_MAC
-(void)createColor:(NSColor*)color;

#endif
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problems you're seeing arise from the fact that TARGET_OS_IPHONE is defined as a variant of TARGET_OS_MAC. (In other words, TARGET_OS_IPHONE is a more-specific case of TARGET_OS_MAC. Or TARGET_OS_MAC is to a rectangle as TARGET_OS_IPHONE is to a square).

So the following code errors out when compiling for iOS because iOS would match both of those conditions, but NSColor is not defined for iOS.

#if TARGET_OS_MAC
-(void)createColor:(NSColor*)color;    

#elif TARGET_OS_IPHONE
-(void)createColor:(UIColor*)color;

#endif

The following code works properly for both because for iOS, it matches the first case, and for Mac OS X, it doesn't match the first but does match the second.

#if TARGET_OS_IPHONE
-(void)createColor:(UIColor*)color;

#elif TARGET_OS_MAC
-(void)createColor:(NSColor*)color;

#endif

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...