How to read .doc or .pdf file with Swift in macOS?

I want to read file content using swift in mac OS, but I am able to read only txt file content using

do {
 readString = try String(contentsOf: path1) 
print(readString) 
}

Replies

For PDF documents viewing, you can use CGPDFDocument.


PDF Document Creation, Viewing, and Transforming.


For Word (and other MS Office) documents, you could use QuickLook.

I want to read file content into a variable in swift. Please help

I want to read file content into a variable in swift.

The reason why

String(contentsOf:)
fails is that these are binary files and String can’t cope with arbitrary binary data. To load binary data, use the
Data(contentsOf:)
. However, that’ll just give you a blob of bytes. How you proceed from there depends on your further goạls, but ksigiscar@cmc suggestions seem pretty relevant to me.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"