#ifdef下的Objective-C代码在Swift中不可见[重复]

时间:2023-01-23 17:48:17

This question already has an answer here:

这个问题在这里已有答案:

I'm adding some swift classes to Objective-C project. The problem is that code under #ifdef in Objective-C classes is not visible in Swift classes. I've defined some static properties to reflect #defines but it is very big overhead to go over whole project and to do that manually. Is there any easier and correct way to do that?

我正在为Objective-C项目添加一些快速类。问题是在Objective-C类中的#ifdef下的代码在Swift类中是不可见的。我已经定义了一些静态属性来反映#defines,但是对整个项目进行手动操作是非常大的开销。有没有更简单,更正确的方法呢?

Note: the question is not about how to do in swift... This is about how to handle already existent defines in objective-c to make them visible in swift.

注意:问题不在于如何在swift中执行...这是关于如何处理objective-c中已存在的定义以使它们在swift中可见。

2 个解决方案

#1


1  

Instead of using #defines why don't you define a class containing const's, then you can switch between object references for different compiles with few lines.

为什么不定义包含const的类,而不是使用#defines,那么您可以在几行之间切换不同编译的对象引用。

#2


0  

There has been a new build settings introduced with Xcode 8: Active Compilation Conditions

Xcode 8:Active Compilation Conditions中引入了新的构建设置

New setting: SWIFT_ACTIVE_COMPILATION_CONDITIONS

新设置:SWIFT_ACTIVE_COMPILATION_CONDITIONS

“Active Compilation Conditions” is a new build setting for passing conditional compilation flags to the Swift compiler.

Previously, we had to declare your conditional compilation flags under OTHER_SWIFT_FLAGS, remembering to prepend “-D” to the setting. For example, to conditionally compile with a MYFLAG value:

以前,我们必须在OTHER_SWIFT_FLAGS下声明您的条件编译标志,记住在设置前加上“-D”。例如,要使用MYFLAG值进行有条件的编译:

#if MYFLAG
// do stuff
#endif

The value to add to the setting -DMYFLAG

要添加到设置-DMYFLAG的值

Now we only need to pass the value MYFLAG to the new setting. Time to move all those conditional compilation values!

现在我们只需要将值MYFLAG传递给新设置。是时候移动所有这些条件编译值了!

Please refer to my answer on another SO post: #ifdef replacement in the Swift language

请参考另一篇SO帖子的答案:用Swift语言替换#ifdef

#1


1  

Instead of using #defines why don't you define a class containing const's, then you can switch between object references for different compiles with few lines.

为什么不定义包含const的类,而不是使用#defines,那么您可以在几行之间切换不同编译的对象引用。

#2


0  

There has been a new build settings introduced with Xcode 8: Active Compilation Conditions

Xcode 8:Active Compilation Conditions中引入了新的构建设置

New setting: SWIFT_ACTIVE_COMPILATION_CONDITIONS

新设置:SWIFT_ACTIVE_COMPILATION_CONDITIONS

“Active Compilation Conditions” is a new build setting for passing conditional compilation flags to the Swift compiler.

Previously, we had to declare your conditional compilation flags under OTHER_SWIFT_FLAGS, remembering to prepend “-D” to the setting. For example, to conditionally compile with a MYFLAG value:

以前,我们必须在OTHER_SWIFT_FLAGS下声明您的条件编译标志,记住在设置前加上“-D”。例如,要使用MYFLAG值进行有条件的编译:

#if MYFLAG
// do stuff
#endif

The value to add to the setting -DMYFLAG

要添加到设置-DMYFLAG的值

Now we only need to pass the value MYFLAG to the new setting. Time to move all those conditional compilation values!

现在我们只需要将值MYFLAG传递给新设置。是时候移动所有这些条件编译值了!

Please refer to my answer on another SO post: #ifdef replacement in the Swift language

请参考另一篇SO帖子的答案:用Swift语言替换#ifdef