Skip to content

Instantly share code, notes, and snippets.

@acwright
Created August 27, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acwright/e4481d76bd397716228b83df9c9be75c to your computer and use it in GitHub Desktop.
Save acwright/e4481d76bd397716228b83df9c9be75c to your computer and use it in GitHub Desktop.
MessageDocument
import SwiftUI
import UniformTypeIdentifiers
struct MessageDocument: FileDocument {
static var readableContentTypes: [UTType] { [.plainText] }
var message: String
init(message: String) {
self.message = message
}
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
message = string
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
return FileWrapper(regularFileWithContents: message.data(using: .utf8)!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment