Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of unresolved identifier 'kUTTypePDF'

Tags:

swift

How to use the file types in Swift

according to https://developer.apple.com/documentation/coreservices/kuttypepdf

this should be completely fine

UIPasteboard.general.setData(Data(contentsOf: URL(fileURLWithPath: path)), forPasteboardType: kUTTypePDF)

yet it yelds

Use of unresolved identifier 'kUTTypePDF'
like image 398
Peter Lapisu Avatar asked Dec 13 '18 11:12

Peter Lapisu


1 Answers

You need to import import MobileCoreServices as "Robert Dresler" said

But you will see below error after import MobileCoreServices

'CFString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?

So you need to do kUTTypePDF as String

After that you may see an error in Data(contentsOf: URL(fileURLWithPath: path) as like below,

Call can throw, but it is not marked with 'try' and the error is not handled

So you need to use try and catch.

You final code will looks like below.

do{

        let data = try Data(contentsOf: URL(fileURLWithPath: path))

        UIPasteboard.general.setData(data, forPasteboardType: kUTTypePDF as String)

    }catch{

        print("error :\(error)")
    }
like image 52
Natarajan Avatar answered Sep 30 '22 03:09

Natarajan