Ios 如何将Objective-C常量作为Swift类型导入?

Ios 如何将Objective-C常量作为Swift类型导入?,ios,objective-c,constants,swift3,Ios,Objective C,Constants,Swift3,我想在swift3中声明常量。例如,如果我们在Objective-C中有以下常数: HK_EXTERN NSString * const HKQuantityTypeIdentifierHeight; HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMass; 我相信可以使用let关键字声明的Swift语言。添加新的Swift文件:常量。Swift import Foundation import HealthKit let HK

我想在
swift3
中声明常量。例如,如果我们在Objective-C中有以下常数:

HK_EXTERN NSString * const HKQuantityTypeIdentifierHeight;
HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMass;

我相信可以使用let关键字声明的Swift语言。

添加新的Swift文件:常量。Swift

import Foundation
import HealthKit

let HK_EXTERN_Height : NSString = HKQuantityTypeIdentifierHeight
let HK_EXTERN_BodyMass : NSString = HKQuantityTypeIdentifierBodyMass
print(HK_EXTERN_Height)
print(HK_EXTERN_BodyMass)
在ViewController.swift中使用

import Foundation
import HealthKit

let HK_EXTERN_Height : NSString = HKQuantityTypeIdentifierHeight
let HK_EXTERN_BodyMass : NSString = HKQuantityTypeIdentifierBodyMass
print(HK_EXTERN_Height)
print(HK_EXTERN_BodyMass)

希望这对您有所帮助……

在Swift中也可以将常量添加到单独的文件中

对于使用常量,如果使用
structs
,则更为智能,因为大量常量可能会造成混乱:

import HealthKit

struct Constants {

    struct Health {

        struct Quantities {
            let height : NSString = HKQuantityTypeIdentifierHeight
            let bodyMass : NSString = HKQuantityTypeIdentifierBodyMass
            static let minWeight: Double = 30.0
        }  
        ...          
    } 
    ...
}
用法:

print(Constants.Health.Quantities.minWeight)


是否要声明全局常量?Thanx:)以获取帮助me@MohsinQureshi随时:)