如何使用Swift的objective - c#定义?

时间:2023-01-15 18:33:57

I am migrating a UIViewController class 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 #define directives.

我正在迁移一个UIViewController类以快速地训练一些。通过桥接头,我成功地使用了Objective-C代码,但是我需要导入一个包含#define指令的常量文件。

I have seen in Using Swift with Cocoa and Objective-C (Simple macros) the following:

我曾见过用Cocoa和Objective-C(简单宏)来使用Swift:

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. For example, the constant definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift with let 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表示,让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 .swift file, cannot be resolved.

所以,这似乎是有可能的。我已经将包含我的常量的文件导入到桥接头中,但是我的.swift文件中没有任何可见性,无法解决。

What should I do to make my constants visible to Swift?

我应该怎样做才能使我的常量对Swift可见?

UPDATE:

更新:

It seems working with NSString constants, but not with booleans:

它似乎与NSString常量一起使用,但不使用布尔值:

#define kSTRING_CONSTANT @"a_string_constant" // resolved from swift
#define kBOOL_CONSTANT YES // unresolved from swift

5 个解决方案

#1


44  

At the moment, some #defines are converted and some aren't. More specifically:

目前,一些#定义了转换,有些则没有。更具体地说:

#define A 1

...becomes:

…就变成:

var A: CInt { get }

Or:

或者:

#define B @"b"

...becomes:

…就变成:

var B: String { get }

Unfortunately, YES and NO aren't recognized and converted on the fly by the Swift compiler.

不幸的是,是的,并且没有被快速编译器识别和转换。

I suggest you convert your #defines to actual constants, which is better than #defines anyway.

我建议您将#定义为实际的常量,这比#定义好。

.h:

. h:

extern NSString* const kSTRING_CONSTANT;
extern const BOOL kBOOL_CONSTANT;

.m

00

NSString* const kSTRING_CONSTANT = @"a_string_constant";
const BOOL kBOOL_CONSTANT = YES;

And then Swift will see:

然后斯威夫特会看到:

var kSTRING_CONSTANT: NSString!
var kBOOL_CONSTANT: ObjCBool

Another option would be to change your BOOL defines to

另一个选择是改变你的BOOL定义。

#define kBOOL_CONSTANT 1

Faster. But not as good as actual constants.

得更快。但不如实际常数好。

#2


19  

Just a quick clarification on a few things from above.

简单地从上面的几件事情上做个简单的说明。

Swift Constant are expressed using the keywordlet

快速常数用关键字表示。

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 }
    }

#3


7  

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)
 }

#4


0  

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"

导入基础让BASE_URL = "www.google.com"

class test {

类测试{

}

}

#5


-1  

simple swift language don't need an macros all #define directives. will be let and complex macros should convert to be func

简单的swift语言不需要所有的#define指令。将会让复杂的宏转换为func ?

#1


44  

At the moment, some #defines are converted and some aren't. More specifically:

目前,一些#定义了转换,有些则没有。更具体地说:

#define A 1

...becomes:

…就变成:

var A: CInt { get }

Or:

或者:

#define B @"b"

...becomes:

…就变成:

var B: String { get }

Unfortunately, YES and NO aren't recognized and converted on the fly by the Swift compiler.

不幸的是,是的,并且没有被快速编译器识别和转换。

I suggest you convert your #defines to actual constants, which is better than #defines anyway.

我建议您将#定义为实际的常量,这比#定义好。

.h:

. h:

extern NSString* const kSTRING_CONSTANT;
extern const BOOL kBOOL_CONSTANT;

.m

00

NSString* const kSTRING_CONSTANT = @"a_string_constant";
const BOOL kBOOL_CONSTANT = YES;

And then Swift will see:

然后斯威夫特会看到:

var kSTRING_CONSTANT: NSString!
var kBOOL_CONSTANT: ObjCBool

Another option would be to change your BOOL defines to

另一个选择是改变你的BOOL定义。

#define kBOOL_CONSTANT 1

Faster. But not as good as actual constants.

得更快。但不如实际常数好。

#2


19  

Just a quick clarification on a few things from above.

简单地从上面的几件事情上做个简单的说明。

Swift Constant are expressed using the keywordlet

快速常数用关键字表示。

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 }
    }

#3


7  

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)
 }

#4


0  

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"

导入基础让BASE_URL = "www.google.com"

class test {

类测试{

}

}

#5


-1  

simple swift language don't need an macros all #define directives. will be let and complex macros should convert to be func

简单的swift语言不需要所有的#define指令。将会让复杂的宏转换为func ?