创建iOS / OS X跨平台类

时间:2022-06-04 03:00:22

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.

我不想最终得到一个静态库,我想用iOS的方法和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?

我遇到的第一个问题是我找不到使用仅在特定系统中可用的类的方法。那么,例如,我如何管理在iOS中使用UIColor和在OS X中使用NSColor的功能?

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

如果我为iOS创建项目,当我查看Foundation.h时,我在标题列表中找不到NSColor.h。

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?

我想用TARGET_OS_MAC和TARGET_OS_IPHONE定义来区分两个系统......我的方法是正确的吗?

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.

目前我有2个目标:iOSTestApp和OSxTestApp。对于这些目标,我已根据系统包含了所需的框架。

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)

仅当我选择OSXTestApp作为活动目标时,才使用TARGET_OS_MAC和TARGET_OS_IPHONE。当我选择iOSAppTest时,Xcode会返回OS X数据类型的错误(即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

3 个解决方案

#1


21  

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).

您看到的问题源于TARGET_OS_IPHONE被定义为TARGET_OS_MAC的变体。 (换句话说,TARGET_OS_IPHONE是TARGET_OS_MAC的更具体的情况。或者TARGET_OS_MAC是一个矩形,因为TARGET_OS_IPHONE是一个正方形)。

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.

因此,以下代码在编译iOS时会出错,因为iOS会匹配这两个条件,但NSColor没有为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.

以下代码适用于两者,因为对于iOS,它与第一种情况匹配,对于Mac OS X,它与第一种情况不匹配但与第二种情况匹配。

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

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

#endif

#2


5  

When it comes to UIColor/NSColor, I handle it like this:

说到UIColor / NSColor,我这样处理:

#if TARGET_OS_IPHONE
#define ImageClassName UIImage
#else
#define ImageClassName NSImage
#endif

Then, in header files, and if you're just passing instances around, you can just use e.g. ImageClassName *.

然后,在头文件中,如果你只是传递实例,你可以使用例如ImageClassName *。

Repeat the #if block in your code when you need to use the UIColor/NSColor APIs.

需要使用UIColor / NSColor API时,在代码中重复#if块。

#3


4  

the two defines you talk about are set to 1 or 0 depending on what you compile for! (at compilation time)

根据你编译的内容,你所谈论的两个定义被设置为1或0! (编译时)

so you are on the right track here I guess

所以我猜你在这里是正确的轨道

e.g.

例如

#if TARGET_OS_IPHONE
#import <CoreText/CoreText.h>
#elif TARGET_OS_MAC
#import <ApplicationServices/ApplicationServices.h>
#endif

#1


21  

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).

您看到的问题源于TARGET_OS_IPHONE被定义为TARGET_OS_MAC的变体。 (换句话说,TARGET_OS_IPHONE是TARGET_OS_MAC的更具体的情况。或者TARGET_OS_MAC是一个矩形,因为TARGET_OS_IPHONE是一个正方形)。

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.

因此,以下代码在编译iOS时会出错,因为iOS会匹配这两个条件,但NSColor没有为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.

以下代码适用于两者,因为对于iOS,它与第一种情况匹配,对于Mac OS X,它与第一种情况不匹配但与第二种情况匹配。

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

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

#endif

#2


5  

When it comes to UIColor/NSColor, I handle it like this:

说到UIColor / NSColor,我这样处理:

#if TARGET_OS_IPHONE
#define ImageClassName UIImage
#else
#define ImageClassName NSImage
#endif

Then, in header files, and if you're just passing instances around, you can just use e.g. ImageClassName *.

然后,在头文件中,如果你只是传递实例,你可以使用例如ImageClassName *。

Repeat the #if block in your code when you need to use the UIColor/NSColor APIs.

需要使用UIColor / NSColor API时,在代码中重复#if块。

#3


4  

the two defines you talk about are set to 1 or 0 depending on what you compile for! (at compilation time)

根据你编译的内容,你所谈论的两个定义被设置为1或0! (编译时)

so you are on the right track here I guess

所以我猜你在这里是正确的轨道

e.g.

例如

#if TARGET_OS_IPHONE
#import <CoreText/CoreText.h>
#elif TARGET_OS_MAC
#import <ApplicationServices/ApplicationServices.h>
#endif