I want to declare constant in Swift 3
. For example if we have following constant in Objective-C :
我想在Swift 3中声明常量。例如,如果我们在Objective-C中有以下常量:
HK_EXTERN NSString * const HKQuantityTypeIdentifierHeight;
HK_EXTERN NSString * const HKQuantityTypeIdentifierBodyMass;
3 个解决方案
#1
1
Add New Swift file : Constant.swift
添加新的Swift文件:Constant.swift
import Foundation
import HealthKit
let HK_EXTERN_Height : NSString = HKQuantityTypeIdentifierHeight
let HK_EXTERN_BodyMass : NSString = HKQuantityTypeIdentifierBodyMass
Use in ViewController.swift
在ViewController.swift中使用
print(HK_EXTERN_Height)
print(HK_EXTERN_BodyMass)
Hope this help you....
希望这能帮到你....
#2
1
Adding constants into a separate file is also possible in Swift.
在Swift中也可以将常量添加到单独的文件中。
For using constants, it is even smarter, if you use structs
, because huge number of constants can be messy:
对于使用常量,如果你使用结构,它甚至更聪明,因为大量的常量可能很乱:
import HealthKit
struct Constants {
struct Health {
struct Quantities {
let height : NSString = HKQuantityTypeIdentifierHeight
let bodyMass : NSString = HKQuantityTypeIdentifierBodyMass
static let minWeight: Double = 30.0
}
...
}
...
}
Usage:
用法:
print(Constants.Health.Quantities.minWeight)
or
要么
let healthNumbers = Constants.Health.Quantities
print(healthNumbers.minWeight)
#3
0
I believe in the Swift language you can declare using let key word.
我相信您可以使用let关键字声明Swift语言。
#1
1
Add New Swift file : Constant.swift
添加新的Swift文件:Constant.swift
import Foundation
import HealthKit
let HK_EXTERN_Height : NSString = HKQuantityTypeIdentifierHeight
let HK_EXTERN_BodyMass : NSString = HKQuantityTypeIdentifierBodyMass
Use in ViewController.swift
在ViewController.swift中使用
print(HK_EXTERN_Height)
print(HK_EXTERN_BodyMass)
Hope this help you....
希望这能帮到你....
#2
1
Adding constants into a separate file is also possible in Swift.
在Swift中也可以将常量添加到单独的文件中。
For using constants, it is even smarter, if you use structs
, because huge number of constants can be messy:
对于使用常量,如果你使用结构,它甚至更聪明,因为大量的常量可能很乱:
import HealthKit
struct Constants {
struct Health {
struct Quantities {
let height : NSString = HKQuantityTypeIdentifierHeight
let bodyMass : NSString = HKQuantityTypeIdentifierBodyMass
static let minWeight: Double = 30.0
}
...
}
...
}
Usage:
用法:
print(Constants.Health.Quantities.minWeight)
or
要么
let healthNumbers = Constants.Health.Quantities
print(healthNumbers.minWeight)
#3
0
I believe in the Swift language you can declare using let key word.
我相信您可以使用let关键字声明Swift语言。