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/