Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use UTTypePlainText as a String in Swift?

I have a project that used to support pre-iOS 15 versions but now only supports iOS 15+. Since the change, I received the following Xcode warning for the following line of code:

let itemProvider = NSItemProvider(item: NSData(data: data), typeIdentifier: kUTTypePlainText as String)

‘kUTTypePlainText’ was deprecated in iOS 15.0: Use UTTypePlainText instead.

Substituting UTTypePlainText in does not remedy the warning because it cannot find it in scope. The following also does not work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let itemProvider = NSItemProvider(item: NSData(data: data), typeIdentifier: UTType.plainText)

I can’t find any clear documentation on this type. I’ve imported UniformTypeIdentifiers, CoreFoundation, CoreServices, and MobileCoreServices to no avail.

>Solution :

After importing UniformTypeIdentifiers you can use UTType.plainText to get the needed UTType. Then use its identifier property to get the String.

So your line becomes:

let itemProvider = NSItemProvider(item: NSData(data: data), typeIdentifier: UTType.plainText.identifier)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading