0

I need to convert an enum to a string, using this variable:

var bloodType:HKBloodTypeObject? = healthKitStore.bloodTypeWithError(&error);

And this enum:

enum HKBloodType : Int {
    case NotSet
    case APositive
    case ANegative
    case BPositive
    case BNegative
    case ABPositive
    case ABNegative
    case OPositive
    case ONegative
}

I know that there are other questions similar to this, but I haven't found any answers that worked for me.

kdopen
  • 8,032
  • 7
  • 44
  • 52
htlee1500
  • 189
  • 1
  • 1
  • 8
  • see http://stackoverflow.com/questions/1094984/convert-objective-c-typedef-to-its-string-equivalent –  Jul 17 '15 at 22:19

2 Answers2

1

As simple description: the HKBloodTypeObject as wrapper of HKBloodType parameter that is in the HealthKit storage.

extension HKBloodTypeObject {
    func string()->String {
        switch self.bloodType {
        case .abNegative:
            return "AB-"
        case .abPositive:
            return "AB+"
        case .aNegative:
            return "A-"
        case .aPositive:
            return "A+"
        case .bNegative:
            return "B-"
        case .bPositive:
            return "B+"
        case .oNegative:
            return "O-"
        case .oPositive:
            return "O+"
        default:
            return "Not Set"
        }
    }
}

the best way to use this extension of HKBloodType enum. Hope lines of code above 'll help you.

DmitryKh
  • 21
  • 4
0

Create a en extension to HKBloodType that implements CustomStringConvertible (Printable for Swift < 2), see here: https://stackoverflow.com/a/24707744/335974

Community
  • 1
  • 1
Felipe Cypriano
  • 2,727
  • 22
  • 32