Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将Objective-C外部常量迁移到Swift_Ios_Swift_Global Variables - Fatal编程技术网

Ios 如何将Objective-C外部常量迁移到Swift

Ios 如何将Objective-C外部常量迁移到Swift,ios,swift,global-variables,Ios,Swift,Global Variables,我在.h文件中有一个类似于以下内容的Objective-C类: @interface GlobalTags : NSObject extern const BOOL showMessages; @end @implementation GlobalTags const BOOL showMessages = YES; @end 以及类似于以下内容的.m文件: @interface GlobalTags : NSObject extern const BOOL showMes

我在
.h
文件中有一个类似于以下内容的Objective-C类:

@interface GlobalTags : NSObject
   extern const BOOL showMessages;
@end
@implementation GlobalTags
   const BOOL showMessages = YES;
@end
以及类似于以下内容的
.m
文件:

@interface GlobalTags : NSObject
   extern const BOOL showMessages;
@end
@implementation GlobalTags
   const BOOL showMessages = YES;
@end
总之,我想要的是有几个全局变量,而不需要实例化一个类,只需要通过导入它们的文件来访问它们。在Swift中执行此操作的最佳方式是什么

谢谢

编辑:关于从Objective-C代码中获取Swift全局变量,这是不可能的(见此)

为了使这些全局变量在Objective-C代码中可见,我在Swift文件中这样做:

class GlobalTags: NSObject {
   static let showMessages: Bool = true;
}
let kStringConstKey : String = "YourKey"
let kBoolConstKey : Bool = true
let kIntConstKey : Int = 34
let kDoubleConstKey : Double = 25.0

这是正确的/最好的方法吗?

很简单。在Constants.swift文件中:

class GlobalTags: NSObject {
   static let showMessages: Bool = true;
}
let kStringConstKey : String = "YourKey"
let kBoolConstKey : Bool = true
let kIntConstKey : Int = 34
let kDoubleConstKey : Double = 25.0

制作一个类似文件->ios->Swift文件的文件。 它将创建空文件


现在,我可以从应用程序中的任何位置访问用户。

无需在类或其他数据结构中定义它们?不需要。在常数.swift文件中。只需删除类主体并简单地写下这些行。没有任何类或数据结构。更多。如果Swift是在Swift和同一项目工作区中开发的,则Swift不需要导入文件。将任何Swift文件用于目标C或将任何目标文件用于Swift。你必须使用桥接头。这里有一些例子如何做到这一点,我在这里找到了回应:这可能是真实的,如果该项目只是与swift。如果Swift和Objective-C混合使用,则不能使用Objective-C中的变量