benwiggy benwiggy - 3 years ago 320
Swift Question

Converting a rawValue into a string in Swift

Apple has made changes from Swift 3 to 4. When I run the following code:

let metadata = [ PDFDocumentAttribute.titleAttribute,
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")

I now get: PDFDocumentAttribute(_rawValue: Title) instead of "Title", which I got before as the value of

How do I get rid of the rawValue stuff?

Answer Source

The PDFDocumentAttribute type has a property called rawValue that contains the old string value. So you can say

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

As an aside, instead of force-unwrapping the attribute you can use an if let, as in

if let attr = attributes[value] {
    print("\(metadata[index].rawValue): \(attr)")
} else {
    print("\(metadata[index].rawValue): nil")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download