在 WatchKit 和主机应用程序中使用具有不同标志的相同文件 [英] Using the same file with different flags in WatchKit and host App

查看:20
本文介绍了在 WatchKit 和主机应用程序中使用具有不同标志的相同文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 watchkit 扩展和主机应用程序中使用相同的代码,但在主机应用程序中使用了一些额外的代码,而在 watchkit 扩展中使用了一些额外的代码.为此,我在各自的目标上添加了 WATCH 和 APP swift 标志.问题是,当我在选择了 App 方案的情况下查看我的代码时,它不会在语法上突出显示 APP 代码,但会突出显示 WATCH 代码,而其他引用该 APP 代码的代码则无法编译.

I'm trying to use the same code in both my watchkit extension and host App, but with some additional code in the host App and some additional code in the watchkit extension. To do this I've added WATCH and APP swift flags on the respective targets. Problem is, when I look at my code with my App scheme selected, it doesn't syntax highlight the APP code but does highlight the WATCH code, and other code that refers to the APP code then fails to compile.

watchkit 扩展是应用程序的目标依赖项,所以我猜它就像是为手表编译代码,然后为应用程序使用相同的编译代码,尽管在编译结果中我可以看到它是使用正确的标志编译并且看不到 watchkit 和 App 构建路径之间的任何重叠,有什么想法吗?

The watchkit extension is a target dependency of the App so I'm guessing it's something like it is compiling the code for the watch and then using the same compiled code for the App, although in the compile results I can see it is compiling with the correct flag and can't see any overlap between watchkit and App build paths, any ideas?

推荐答案

SWIFT 版本

在 WatchKit 扩展目标的构建设置中使用其他 swift 标志.例如,添加一个标志 WATCH(必须以 -D 为前缀):

Use other swift flags in build settings of your WatchKit Extension target. For example, add a flag WATCH (must be prefixed with -D):

然后在您的共享文件中添加以下代码:

Then in your shared file add this code:

        #if WATCH
            NSLog("watch")
        #else
            NSLog("app")
        #endif

Objective-C 版本

在 WatchKit 扩展目标的构建设置中使用预处理器宏.比如添加一个宏WATCH = 1:

Use preprocessor macros in build settings of your WatchKit Extension target. For example, add a macro WATCH = 1:

然后在您的共享文件中添加以下代码:

Then in your shared file add this code:

#ifdef WATCH
    NSLog(@"watch");
#else
    NSLog(@"app");
#endif

这篇关于在 WatchKit 和主机应用程序中使用具有不同标志的相同文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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