swift - 在 Swift 中将 rawValue 转换为字符串

标签 swift

Apple 已将 Swift 3 更改为 4。当我运行以下代码时:

let metadata = [ PDFDocumentAttribute.titleAttribute,
                 PDFDocumentAttribute.authorAttribute,
                 PDFDocumentAttribute.subjectAttribute,
                 PDFDocumentAttribute.creatorAttribute,
                 PDFDocumentAttribute.producerAttribute,
                 PDFDocumentAttribute.creationDateAttribute,
                 PDFDocumentAttribute.modificationDateAttribute,
                 PDFDocumentAttribute.keywordsAttribute ]

if var attributes = pdfDoc.documentAttributes {

    for (index, value) in metadata.enumerated() {
        if attributes[value] != nil {

            print("\(metadata[index])): \(String(describing: attributes[value]!))")
        }    else {
            print("\(metadata[index]): nil")
        }
    }

我现在得到:PDFDocumentAttribute(_rawValue: Title) 而不是“Title”,这是我之前作为 metadata[index] 的值得到的。

我如何摆脱 rawValue 的东西?

最佳答案

PDFDocumentAttribute 类型有一个名为 rawValue 的属性,其中包含旧字符串值。所以你可以说

print("\(metadata[index].rawValue): \(String(describing: attributes[value]!))")

顺便说一句,您可以使用 if let 而不是强制展开属性,如

if let attr = attributes[value] {
    print("\(metadata[index].rawValue): \(attr)")
} else {
    print("\(metadata[index].rawValue): nil")
}

关于swift - 在 Swift 中将 rawValue 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46531976/

相关文章:

iOS链接两个firebase帐户错误

ios - Crashlytics forceCrash 不工作

ios - 如何将特定图像设置到特定数组位置?

ios - 仅在异步函数执行完毕后运行代码

ios - 如何在完成 block 之前解决嵌套的异步调用

ios - 在 Swift 中将 NSURLSession 与 IOS 上的 HTTP REST API 结合使用

ios - 如何在 uicollectionview 中设置我的 UIButton 标题

ios - 尝试将 Parse 中的一行数据提取到 Swift 中的数组中

swift - 使用自动布局时遇到问题 - 为什么在堆栈 View 下添加标签时会出现错误?

swift - 在 Swift 中十进制、二进制和十六进制之间转换