如何使用 Swift 中的 Objective-C #define
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24325477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a Objective-C #define from Swift
提问by atxe
I am migrating a UIViewControllerclass to train a bit with Swift. I am successfully using Objective-C code via the bridging header but I have the need of importing a constants file that contains #definedirectives.
我正在迁移一个UIViewController班级以使用 Swift 进行一些训练。我通过桥接头成功地使用了 Objective-C 代码,但我需要导入一个包含#define指令的常量文件。
I have seen in Using Swift with Cocoa and Objective-C(Simple macros) the following:
我在Using Swift with Cocoa and Objective-C(Simple macros) 中看到了以下内容:
Simple Macros
Where you typically used the
#definedirective to define a primitive constant in C and Objective-C, in Swift you use a global constant instead. For example, the constant definition#define FADE_ANIMATION_DURATION 0.35can be better expressed in Swift withlet 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.
简单的宏
#define在 C 和 Objective-C 中,您通常使用指令来定义原始常量,而在 Swift 中,您使用全局常量代替。例如,常量定义#define FADE_ANIMATION_DURATION 0.35可以用 Swift 更好地表达let FADE_ANIMATION_DURATION = 0.35。因为简单的类似常量的宏直接映射到 Swift 全局变量,所以编译器会自动导入在 C 和 Objective-C 源文件中定义的简单宏。
So, it seems it's possible. I have imported the file containing my constants into the bridging header, but I have no visibility from my .swiftfile, cannot be resolved.
所以,似乎是有可能的。我已将包含我的常量的文件导入到桥接头中,但我的.swift文件中没有可见性,无法解析。
What should I do to make my constants visible to Swift?
我应该怎么做才能让我的常量对 Swift 可见?
UPDATE:
更新:
It seems working with NSStringconstants, but not with booleans:
它似乎适用于NSString常量,但不适用于布尔值:
#define kSTRING_CONSTANT @"a_string_constant" // resolved from swift
#define kBOOL_CONSTANT YES // unresolved from swift
采纳答案by fabrice truillot de chambrier
At the moment, some #defines are converted and some aren't. More specifically:
目前,有些#defines 已转换,有些未转换。进一步来说:
#define A 1
...becomes:
...变成:
var A: CInt { get }
Or:
或者:
#define B @"b"
...becomes:
...变成:
var B: String { get }
Unfortunately, YESand NOaren't recognized and converted on the fly by the Swift compiler.
遗憾的是,YES并NO没有识别和转换由斯威夫特编译器的飞行。
I suggest you convert your #defines to actual constants, which is better than #defines anyway.
我建议您将#defines转换为实际常量,这#define无论如何都比s好。
.h:
。H:
extern NSString* const kSTRING_CONSTANT;
extern const BOOL kBOOL_CONSTANT;
.m
.m
NSString* const kSTRING_CONSTANT = @"a_string_constant";
const BOOL kBOOL_CONSTANT = YES;
And then Swift will see:
然后 Swift 会看到:
var kSTRING_CONSTANT: NSString!
var kBOOL_CONSTANT: ObjCBool
Another option would be to change your BOOLdefines to
另一种选择是将您的BOOL定义更改为
#define kBOOL_CONSTANT 1
Faster. But not as good as actual constants.
快点。但不如实际常量好。
回答by Joshua
Just a quick clarification on a few things from above.
只是从上面快速澄清一些事情。
Swift Constant are expressed using the keywordlet
Swift 常量使用关键字表示let
For Example:
例如:
let kStringConstant:String = "a_string_constant"
Also, only in a protocol definition can you use { get }, example:
此外,只有在协议定义中才能使用{ get },例如:
protocol MyExampleProtocol {
var B:String { get }
}
回答by Ankish Jain
In swift you can declare an enum, variable or function outside of any class or function and it will be available in all your classes (globally)(without the need to import a specific file).
在 swift 中,您可以在任何类或函数之外声明一个枚举、变量或函数,并且它将在您的所有类(全局)中可用(无需导入特定文件)。
import Foundation
import MapKit
let kStringConstant:String = "monitoredRegions"
class UserLocationData : NSObject {
class func getAllMonitoredRegions()->[String]{
defaults.dictionaryForKey(kStringConstant)
}
回答by Amr Angry
simple swift language don't need an macros all #define directives. will be let and complex macros should convert to be func
简单的 swift 语言不需要所有 #define 指令的宏。将被 let 和复杂的宏应该转换为 func
回答by Ankit garg
The alternative for macro can be global variable . We can declare global variable outside the class and access those without using class. Please find example below
宏的替代方案可以是全局变量。我们可以在类之外声明全局变量并在不使用类的情况下访问它们。请在下面找到示例
import Foundation
let BASE_URL = "www.google.com"
class test {
}

