简体   繁体   English

打印导入的枚举大小写会给出枚举名称而不是大小写名称

[英]Printing imported enum case gives enum name instead of case name

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: 根据文档和该线程以及其他内容,对于一个Ints枚举,我可以通过简单地执行以下操作将案例名称打印为字符串:

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: 但是,如果我尝试使用CKAccountStatus做同样的事情,它将显示枚举的名称:

import CloudKit

let testStatus = CKAccountStatus.noAccount

print(testStatus)

// prints "CKAccountStatus"

CKAccountStatus is an enum of Ints, just like the test enum above: CKAccountStatus是一个Ints枚举,就像上面的测试枚举一样:

public enum CKAccountStatus : Int {


    case couldNotDetermine

    case available

    case restricted

    case noAccount
}

What am I doing wrong and/or why is this happening? 我在做什么错和/或为什么会这样?

Your TestEnum is a swift enum. 您的TestEnum是一个快速的枚举。 CKAccountStatus could be Objective C enum. CKAccountStatus可以是目标C枚举。

You can achieve it by confirming the CustomStringConvertible protocol by adding: 您可以通过添加以下内容来确认CustomStringConvertible协议来实现此目的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM