2014-10-03 1 views
60

Semplicemente ho una struct che memorizza le costanti di applicazione come di seguito:Come utilizzare struct Swift in Objective C

struct Constant { 

    static let ParseApplicationId = "xxx" 
    static let ParseClientKey = "xxx" 

    static var AppGreenColor: UIColor { 
     return UIColor(hexString: "67B632") 
    } 
} 

Queste costanti possono essere utilizzati nel codice Swift chiamando Constant.ParseClientKey per esempio. Ma nel mio codice, contiene anche alcune classi Objective C. Quindi la mia domanda è come usare queste costanti nel codice Objective C?

Se questo modo di dichiarare le costanti non è buono, qual è il modo migliore per creare costanti globali da utilizzare sia nel codice Swift che in quello Objective C?

+9

segui comune stile di codice swift e utilizzare un lettera minuscola per avviare gli identificativi let/var. –

+2

@NikolaiRuhe Questo non sarebbe lo stile corretto per le proprietà statiche di una struttura? Molto simile a "UIControlEvents.TouchUpInside"? –

+0

@LukeRogers '.TouchUpInside' è un membro di enumerazione, non una proprietà di una struttura. –

risposta

75

Triste a dirsi, non è possibile esporre struct, né le variabili globali a Objective-C. vedi the documentation.

A partire da ora, secondo me, il modo migliore è qualcosa di simile:

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, è possibile utilizzare in questo modo:

NSString *appklicationId = [Constant parseApplicationId]; 
NSString *clientKey = [Constant parseClientKey]; 
UIColor *greenColor = [Constant appGreenColor]; 
+0

Accessibile anche a Swift? – khunshan

+0

@khunshan Sì. Accessibile direttamente con 'ParseClientKey', o tramite la classe' Constant.clientKey() ' – Fabian

+7

Dovresti usare' '' @objc class Constant: NSObject''' –

7
//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. 
0

Si dovrebbe fare le istruzioni let sono private se si desidera rendere altri tipi Swift nel proprio codice per accedere a queste costanti solo tramite la classe:

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, è possibile utilizzare in questo modo:

UIColor *greenColor = Constant.appGreenColor 

La seguente riga non compilerà più ora, poiché l'istruzione let è privato:

UIColor *greenColor = appGreenColor