如何在Objective C中使用Swift结构

简单地说,我有一个存储应用程序常量的结构如下:

struct Constant { static let ParseApplicationId = "xxx" static let ParseClientKey = "xxx" static var AppGreenColor: UIColor { return UIColor(hexString: "67B632") } } 

例如,通过调用Constant.ParseClientKey ,可以在Swift代码中使用这些常量。 但在我的代码中,它也包含一些Objective C类。 所以我的问题是如何在Objective C代码中使用这些常量?

如果这种声明常量的方法不好,那么创build在Swift和Objective C代码中使用全局常量的最好方法是什么?

可悲的是,你不能将struct和全局variables暴露给Objective-C。 请参阅文档 。

到目前为止,恕我直言,最好的办法是这样的:

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

在Objective-C中,你可以像这样使用它们:

 NSString *appklicationId = [Constant parseApplicationId]; NSString *clientKey = [Constant parseClientKey]; UIColor *greenColor = [Constant appGreenColor]; 
 //Why not create a file something like this: import UIKit import Foundation 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: //#import "ProjectModuleName-Swift.h" //Swift usage: //self.view.backgroundColor = Constant.kAppGreenColor //Objective-C file: //self.view.backgroundColor = [Constants appGreenColor]; //This way you can update colors, default text, web service urls for whole app in one place. //just an idea on this thread. 

如果你想让代码中的其他Swifttypes只通过class来访问这些常量,你应该让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 } } 

在Swift中,你可以像这样使用它们:

 UIColor *greenColor = Constant.appGreenColor 

由于let语句是私有的,所以现在不再编译了:

 UIColor *greenColor = appGreenColor