如何在 Objective-C 中使用 Swift 结构体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26173234/
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 Swift struct in Objective-C
提问by Dinh Quan
Simply I have a struct that stores the application constants as below:
简单地说,我有一个存储应用程序常量的结构,如下所示:
struct Constant {
static let ParseApplicationId = "xxx"
static let ParseClientKey = "xxx"
static var AppGreenColor: UIColor {
return UIColor(hexString: "67B632")
}
}
These constants can be use in Swift code by calling Constant.ParseClientKeyfor example. But in my code, it also contains some Objective-C classes. So my question is how to use these constants in the Objective-C code?
例如,这些常量可以通过调用在 Swift 代码中使用Constant.ParseClientKey。但在我的代码中,它还包含一些 Objective-C 类。所以我的问题是如何在 Objective-C 代码中使用这些常量?
If this way to declare constants is not good then what is the best way to create global constants to be used in both Swift and Objective-C code?
如果这种声明常量的方式不好,那么创建可在 Swift 和 Objective-C 代码中使用的全局常量的最佳方式是什么?
回答by rintaro
Sad to say, you can not expose struct, nor global variables to Objective-C. see the documentation.
遗憾的是,您不能struct向 Objective-C公开或全局变量。请参阅文档。
As of now, IMHO, the best way is something like this:
到目前为止,恕我直言,最好的方法是这样的:
let ParseApplicationId = "xxx"
let ParseClientKey = "xxx"
let AppGreenColor = UIColor(red: 0.2, green: 0.7, blue: 0.3 alpha: 1.0)
@objc class Constant: NSObject {
private init() {}
class func parseApplicationId() -> String { return ParseApplicationId }
class func parseClientKey() -> String { return ParseClientKey }
class func appGreenColor() -> UIColor { return AppGreenColor }
}
In Objective-C, you can use them like this:
在 Objective-C 中,你可以像这样使用它们:
NSString *appklicationId = [Constant parseApplicationId];
NSString *clientKey = [Constant parseClientKey];
UIColor *greenColor = [Constant appGreenColor];
回答by Marijan V.
Why not create a file with both a structand an @objc class, something like this:
为什么不创建一个同时包含 astruct和 an的文件@objc class,如下所示:
import UIKit
extension UIColor {
convenience init(hex: Int) {
let components = (
R: CGFloat((hex >> 16) & 0xff) / 255,
G: CGFloat((hex >> 08) & 0xff) / 255,
B: CGFloat((hex >> 00) & 0xff) / 255
)
self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
}
}
extension CGColor {
class func colorWithHex(hex: Int) -> CGColorRef {
return UIColor(hex: hex).CGColor
}
}
struct Constant {
static let kParseApplicationId = "5678"
static let kParseClientKey = "1234"
static var kAppGreenColor: UIColor { return UIColor(hex:0x67B632) }
static var kTextBlackColor: UIColor { return UIColor(hex:0x000000) }
static var kSomeBgBlueColor: UIColor { return UIColor(hex:0x0000FF) }
static var kLineGrayCGColor: CGColor { return CGColor.colorWithHex(0xCCCCCC) }
static var kLineRedCGColor: CGColor { return CGColor.colorWithHex(0xFF0000) }
}
@objc class Constants: NSObject {
private override init() {}
class func parseApplicationId() -> String { return Constant.kParseApplicationId }
class func parseClientKey() -> String { return Constant.kParseClientKey }
class func appGreenColor() -> UIColor { return Constant.kAppGreenColor }
class func textBlackColor() -> UIColor { return Constant.kTextBlackColor }
class func someBgBlueColor() -> UIColor { return Constant.kSomeBgBlueColor }
class func lineGrayCGColor() -> CGColor { return Constant.kLineGrayCGColor }
class func lineRedCGColor() -> CGColor { return Constant.kLineRedCGColor }
}
For use in Objective-C files add this when you need to use constants:
为了在 Objective-C 文件中使用,当你需要使用常量时添加:
#import "ProjectModuleName-Swift.h"
Swift usage:
迅捷用法:
self.view.backgroundColor = Constant.kAppGreenColor
Objective-C usage:
Objective-C 用法:
self.view.backgroundColor = [Constants appGreenColor];
This way you can update colors, default text, web service urls for whole app in one place.
通过这种方式,您可以在一个地方更新整个应用程序的颜色、默认文本、Web 服务 URL。
回答by ktheitroada
You should make the let statements private if you want to make other Swift types in your code to access these constants only via class:
如果您想让代码中的其他 Swift 类型仅通过类访问这些常量,则应该将 let 语句设为私有:
private let AppGreenColor = UIColor(red: 0.2, green: 0.7, blue: 0.3 alpha: 1.0)
@objc class Constant {
class func appGreenColor() -> UIColor { return AppGreenColor }
}
In Swift, you can use them like this:
在 Swift 中,你可以像这样使用它们:
UIColor *greenColor = Constant.appGreenColor
The following line will not compile anymore now since the let statement is private:
由于 let 语句是私有的,因此现在将不再编译以下行:
UIColor *greenColor = appGreenColor
回答by Anil Arigela
Though this might be late or redundant, I could make it work with the following code :
尽管这可能会迟到或多余,但我可以使用以下代码使其工作:
@objcMembers class Flags: NSObject {
static let abcEnabled = false
static let pqrEnabled = false
.
.
.
}
Obviously, to use in objc c code, you have to do #import "ProjectModuleName-Swift.h"
显然,要在 objc c 代码中使用,您必须执行 #import "ProjectModuleName-Swift.h"

