创建iOS / OS X跨平台类 [英] Creating an iOS/OS X cross-platform class

查看:107
本文介绍了创建iOS / OS X跨平台类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多关于为这两个系统创建跨平台库的问题。每个回答都指向静态库作为解决方案。



我不想结束一个静态库,我想创建一个类的方法为iOS和其对应的OS X。

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

我遇到的第一个问题是,我找不到一种方法来使用可用的类只在特定的系统中。例如,我如何在iOS中使用 UIColor 来管理一个函数,在OS X中使用 NSColor / p>

如果我创建一个iOS项目,当我看到 Foundation.h 我找不到 NSColor.h



我想使用 TARGET_OS_MAC TARGET_OS_IPHONE 定义区分两个系统...我在正确的方式?



编辑以添加更多信息:



:iOSTestApp和OSxTestApp。对于这些目标,我已经包括所需的框架,取决于系统。



使用 TARGET_OS_MAC TARGET_OS_IPHONE OSXTestApp作为活动目标。当我选择iOSAppTest时,Xcode返回OS X数据类型的错误(即 NSColor



产生这些错误的代码:

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

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

#endif

如果我反转定义,
这里是一个产生这些错误的代码示例:

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

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

#endif


解决方案

您遇到的问题是由于 TARGET_OS_IPHONE 被定义为 TARGET_OS_MAC 的变体。 (换句话说, TARGET_OS_IPHONE 是更特定的 TARGET_OS_MAC TARGET_OS_MAC 是一个矩形,因为 TARGET_OS_IPHONE 是一个正方形)。



编译iOS时出错,因为iOS将匹配这些条件,但 NSColor 未针对iOS定义。

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

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

#endif

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

 如果TARGET_OS_IPHONE 
- (void)createColor:(UIColor *)color;

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

#endif


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

解决方案

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

这篇关于创建iOS / OS X跨平台类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆