0

As per documentation and this thread, among others, for an enum of Ints I can print the case name as a string by simply doing this:

enum TestEnum: Int {
    case one
    case two
    case three
}

let testEnum = TestEnum.two

print(testEnum)

// prints "two"

Which works of course. But if I try to do the same thing with CKAccountStatus, it prints the name of the enum:

import CloudKit

let testStatus = CKAccountStatus.noAccount

print(testStatus)

// prints "CKAccountStatus"

CKAccountStatus is an enum of Ints, just like the test enum above:

public enum CKAccountStatus : Int {


    case couldNotDetermine

    case available

    case restricted

    case noAccount
}

What am I doing wrong and/or why is this happening?

Brian M
  • 3,812
  • 2
  • 11
  • 31
  • Your code works fine on Playground XCode 10.2, print "noAccount" – William Hu Mar 30 '19 at 12:21
  • @William Hu. I’m using playground 10.2 and it prints “CKAccountStatus” for me. It prints “noAccount” for you? How is that possible? Did you import CloudKit or define CKAccountStatus in your playground code? Try importing CloudKit – Brian M Mar 30 '19 at 12:23
  • Sorry, I just copy your code. Then I used import CloudKit, yes, it is, it prints CKAccountStatus. – William Hu Mar 30 '19 at 12:29
  • I'll edit the question – Brian M Mar 30 '19 at 12:31
  • I think the point is your `TestEnum` is swift only, but `CKAccountStatus` which is for Objective C also. – William Hu Mar 30 '19 at 12:33

1 Answers1

2

Your TestEnum is a swift enum. CKAccountStatus could be Objective C enum.

You can achieve it by confirming the CustomStringConvertible protocol by adding:

extension CKAccountStatus: CustomStringConvertible {
    public var description: String {
        switch self {
        case .noAccount:
            return "noAccount"
        case .available:
            return "available"
        case .restricted:
            return "restricted"
        case .couldNotDetermine:
            return "couldNotDetermine"
        }
    }
}


let testStatus = CKAccountStatus.available
print(testStatus) // available
William Hu
  • 15,423
  • 11
  • 100
  • 121