gpt4 book ai didi

ios - HKBiologicalSexObject.biologicalSex 返回 HKBiologicalSex 而不是 .Male 或 .Female

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:51 25 4
gpt4 key购买 nike

我已经授权了 HealthKit,我正在像这样从 HealthKitStore 获取 BiologicalSex:

let healthKitStore:HKHealthStore = HKHealthStore()
var biologicalSexObject: HKBiologicalSexObject?
var biologicalSex: HKBiologicalSex?

do {
biologicalSexObject = try healthKitStore.biologicalSex()
biologicalSex = biologicalSexObject!.biologicalSex
} catch _ as NSError {
biologicalSex = nil
print("error reading biological sex")
}

但是,当我尝试打印 biologicalSex 时,它返回 HKBiologicalSex 而不是 .Male.Female

我在几个教程中或多或少地看到过这个确切的代码,所以我想知道在 Swift 2 中是否有任何我应该注意的语法变化。(错误处理已经改变,所以我很好奇是否其他任何值得注意的东西。)

最佳答案

执行此操作需要 biologicalSex = biologicalSexObject!.biologicalSexrawValue。 BiologicalSex 的枚举如下所示:

typedef enum : NSInteger {
HKBiologicalSexNotSet = 0,
HKBiologicalSexFemale,
HKBiologicalSexMale,
HKBiologicalSexOther,
} HKBiologicalSex;

使用这些信息很容易设计一个 switch 语句来覆盖所有可能的值:

switch biologicalSex.rawValue{
case 0:
biologicalSex = nil
case 1:
biologicalSex = "Female"
case 2:
biologicalSex = "Male"
case 3:
biologicalSex = "Other"
default:
biologicalSex = nil
}

关于ios - HKBiologicalSexObject.biologicalSex 返回 HKBiologicalSex 而不是 .Male 或 .Female,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33311992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com