当前位置:网站首页>SwiftUI 教程之使用 DocumentGroup 和 FileDocument 创建基于文本文档的应用程序
SwiftUI 教程之使用 DocumentGroup 和 FileDocument 创建基于文本文档的应用程序
2022-08-05 17:01:00 【知识大胖】
FileDocument
创建一个类型的结构FileDocument。您将收到一条错误消息,要求您添加协议的存根。单击“修复”以添加所需的数据。正如您在下面看到的,该协议允许我们通过其read初始化程序和write函数来读取和写入文档。您还将拥有readableContentTypes静态变量,该变量将帮助我们确定和限制我们希望在应用程序中允许的文件类型。
struct TextDocument: FileDocument {
static var readableContentTypes: [UTType]
init(configuration: ReadConfiguration) throws {
<#code#>
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
<#code#>
}
}
我们接下来要做的是分配我们的readableContentTypestoUTType.plainText并创建一个新的字符串变量来保存用户的文本输入。在初始化程序中,获取文件的内容,然后尝试将其值分配给我们的文本变量。
要写入文档,请使用该file wrapper功能。在函数内部,将文本变量中的字符串转换为 Data。最后,创建一个初始化程序来创建一个新的空文档文件。
import UniformTypeIdentifiers
struct TextDocument: FileDocument {
// File type
st
边栏推荐
- AI target segmentation capability for fast video cutout without green screen
- C语言之链表简单操作(亲测可用)
- 2018-9-21recording our team Fanhao-Shen Resignation
- Good code in the eyes of a compiler engineer: Loop Interchange
- 【 背包九讲——完全背包问题】
- 【知识点】程序性能调优
- JVM内存和垃圾回收-09.对象的实例化内存布局与访问定位
- 【翻译】EF Core 3.1.x, 5.x & 6.x Second Level Cache Interceptor
- Detailed explanation of SSL (1): What is the significance of the small green lock in the URL bar?
- 【工具】简道云零代码开发平台
猜你喜欢
随机推荐
如何打造HR无法拒绝的简历?
什么是常见缺陷枚举 (CWE)
Stored Procedure && Trigger
疏水18碳磷脂磷脂-聚乙二醇-羧基,DSPE-PEG-Acid,CAS:1403744-37-5
TABLE
阿里云赵明山:详解灵活可插拔的渐进式发布框架OpenKruise Rollout
跨越“S型曲线”,华胜天成如何在数字时代开启第二曲线?
远程push记录:
详解 SSL(一):网址栏的小绿锁有什么意义?
Number system conversion (16 questions per day during summer vacation)
Sentinel链路模式规则无效
App与Hap、Entry与feature,bundleName与packge,务必弄明白
电脑重装系统桌面图标变成白色文件怎么恢复?
AI target segmentation capability for fast video cutout without green screen
8.lnmp架构
【案例】3d变换之一个旋转的圆圈
【案例】3d相册
WPF 截图控件之移除控件(九)「仿微信」
Why do so many people discourage software testing?Don't click here if the monthly salary is more than 10,000.
2018-9-21recording our team Fanhao-Shen Resignation