简体   繁体   English

从Objective-C文件中以swift访问常量

[英]Access constant in swift from Objective-C file

I am very new to Swift. 我对Swift很新。 I have one hybrid project in which some classes are in Objective-C and some are Swift. 我有一个混合项目,其中一些类在Objective-C中,一些是Swift。 I have one Constant.h file which is based with Objective-C file. 我有一个基于Objective-C文件的Constant.h文件。 In this header file have mention some constant like bellow 在这个头文件中提到了一些常常像波纹管

#define mainViewBgColor [UIColor colorWithRed:248.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1.0]

But problem is that i can't access this constant in Swift class. 但问题是我无法在Swift类中访问此常量。 See bellow Swift code 请参阅下面的Swift代码

override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = mainViewBgColor //Want to access liek this but this line gives me an error.
        self.loadNavigation();
    }

Please suggest the right way to access constants like this specifically the constant write in Objective-C. 请建议正确访问常量的正确方法,特别是Objective-C中的常量写入。

Note : Please consider that i have Bridging-Header.h file and i have already import the constant file into that bridge file. 注意:请考虑我有Bridging-Header.h文件,我已经将常量文件导入到该桥文件中。 Even i can successfully access simple string constant. 即使我可以成功访问简单的字符串常量。

Edited Alternate Solution : I have found the alternate solution for using constants in swift is to create new swift constant file let say SwiftConstant.swift and define the constant like bellow in this file. 编辑的替代解决方案 :我发现在swift中使用常量的替代解决方案是创建新的swift常量文件,让我们说SwiftConstant.swift并在此文件中定义类似于下面的常量。

let  mainViewBgColor = UIColor(red: 248.0/255.0, green: 247.0/255.0, blue: 247.0/255.0, alpha: 1.0)

now without importing SwiftConstant.swift file anywhere i can use the constant. 现在没有导入SwiftConstant.swift文件我可以使用常量。 i don't know this is right way or not hoping and wait for good answer. 我不知道这是正确的方式,也不希望等待好的答案。

It depends. 这取决于。

From the documentation 从文档中

Simple Macros 简单的宏

Where you typically used the #define directive to define a primitive constant in C and Objective-C, in Swift you use a global constant instead. 在通常使用#define指令在C和Objective-C中定义基本常量的地方,在Swift中使用全局常量。 For example, the constant definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift with let FADE_ANIMATION_DURATION = 0.35. 例如,常量定义#define FADE_ANIMATION_DURATION 0.35可以在Swift中更好地表达,让FADE_ANIMATION_DURATION = 0.35。 Because simple constant-like macros map directly to Swift global variables, the compiler automatically imports simple macros defined in C and Objective-C source files. 因为简单的类似常量的宏直接映射到Swift全局变量,所以编译器会自动导入在C和Objective-C源文件中定义的简单宏。

Complex Macros 复杂的宏

Complex macros are used in C and Objective-C but have no counterpart in Swift. 复杂的宏在C和Objective-C中使用,但在Swift中没有对应的。 Complex macros are macros that do not define constants, including parenthesized, function-like macros. 复杂的宏是不定义常量的宏,包括带括号的,类似函数的宏。 You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. 您可以在C和Objective-C中使用复杂的宏来避免类型检查约束或避免重新输入大量的样板代码。 However, macros can make debugging and refactoring difficult. 但是,宏可能使调试和重构变得困难。 In Swift, you can use functions and generics to achieve the same results without any compromises. 在Swift中,您可以使用函数和泛型来实现相同的结果,而不会有任何妥协。 Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code. 因此,C和Objective-C源文件中的复杂宏不可用于Swift代码。

Source: Using Swift with Cocoa and Objective-C 来源: 使用Swift与Cocoa和Objective-C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM