Created
August 27, 2020 19:38
-
-
Save acwright/e4481d76bd397716228b83df9c9be75c to your computer and use it in GitHub Desktop.
MessageDocument
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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