Skip to content

Commit 6549199

Browse files
committed
Update Example App Storage
1 parent 9339c42 commit 6549199

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Documents/CodeEditSourceEditorExampleDocument.swift

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
import SwiftUI
99
import UniformTypeIdentifiers
1010

11-
struct CodeEditSourceEditorExampleDocument: FileDocument {
12-
var text: String
11+
struct CodeEditSourceEditorExampleDocument: FileDocument, @unchecked Sendable {
12+
enum DocumentError: Error {
13+
case failedToEncode
14+
}
15+
16+
var text: NSTextStorage
1317

14-
init(text: String = "") {
18+
init(text: NSTextStorage = NSTextStorage(string: "")) {
1519
self.text = text
1620
}
1721

@@ -25,11 +29,28 @@ struct CodeEditSourceEditorExampleDocument: FileDocument {
2529
guard let data = configuration.file.regularFileContents else {
2630
throw CocoaError(.fileReadCorruptFile)
2731
}
28-
text = String(decoding: data, as: UTF8.self)
32+
var nsString: NSString?
33+
NSString.stringEncoding(
34+
for: data,
35+
encodingOptions: [
36+
.allowLossyKey: false, // Fail if using lossy encoding.
37+
.suggestedEncodingsKey: [NSUTF8StringEncoding],
38+
.useOnlySuggestedEncodingsKey: true
39+
],
40+
convertedString: &nsString,
41+
usedLossyConversion: nil
42+
)
43+
if let nsString {
44+
self.text = NSTextStorage(string: nsString as String)
45+
} else {
46+
fatalError("Failed to read file")
47+
}
2948
}
3049

3150
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
32-
let data = Data(text.utf8)
51+
guard let data = (text.string as NSString?)?.data(using: NSUTF8StringEncoding) else {
52+
throw DocumentError.failedToEncode
53+
}
3354
return .init(regularFileWithContents: data)
3455
}
3556
}

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct ContentView: View {
3939
var body: some View {
4040
GeometryReader { proxy in
4141
CodeEditSourceEditor(
42-
$document.text,
42+
document.text,
4343
language: language,
4444
theme: theme,
4545
font: font,

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/StatusBar.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct StatusBar: View {
3030
var body: some View {
3131
HStack {
3232
Menu {
33-
IndentPicker(indentOption: $indentOption, enabled: document.text.isEmpty)
33+
IndentPicker(indentOption: $indentOption, enabled: document.text.length == 0)
3434
.buttonStyle(.borderless)
3535
Toggle("Wrap Lines", isOn: $wrapLines)
3636
Toggle("Show Minimap", isOn: $showMinimap)
@@ -106,8 +106,8 @@ struct StatusBar: View {
106106
guard let fileURL else { return nil }
107107
return CodeLanguage.detectLanguageFrom(
108108
url: fileURL,
109-
prefixBuffer: document.text.getFirstLines(5),
110-
suffixBuffer: document.text.getLastLines(5)
109+
prefixBuffer: document.text.string.getFirstLines(5),
110+
suffixBuffer: document.text.string.getLastLines(5)
111111
)
112112
}
113113

0 commit comments

Comments
 (0)