Getting Started with Document-based SwiftUI AppsDocument-based apps let people create documents they can share with others. Examples of document-based apps are text editors, spreadsheets and video editors. Learn the basics of making document-based SwiftUI apps in this article. Creating a Project in XcodeThe iOS, Mac, and Multiplatform project categories have a Document App project template. Select that template and click the Next button to create a document app. Multiplatform document apps automatically use SwiftUI. If you choose an iOS or Mac document app, make sure you choose SwiftUI from the Interface menu. Click the Next button to finish creating the project. The Project ContentsWhen you create a document app SwiftUI project, Xcode creates the following files for the project:
You will most likely want to change the name of the document struct. Examine the Document StructIf you open the file for the document, it should look similar to the following code:
Xcode configures the document to be a plain text editor. Changing the Document TypeUnless you're writing a plain text editor, you'll have to change the document type. Take a look at the code that defines the document type.
Change the variable name from You also must update the document type for the target in Xcode.
There are three sections related to document types in the project editor: Document Types, Exported Type Identifiers, and Imported Type Identifiers. Xcode includes a document type and an imported type identifier for new document app projects. In the Document Types section, give your document type a name in the Name text field. Make sure the value in the Identifier text field matches the identifier you used when defining the In the Imported Type Identifiers section, enter the document type name in the Description text field. Enter the file extension in the Extensions text field, omitting the leading dot. Use the same identifier you set in the Document Types section. If your document type conforms to a base UTI, enter it in the Conforms To text field. If you create a custom file format for your document, you must add an Exported Type Identifier for the document. Enter the same information you entered in the Imported Type Identifiers section: description, extensions, identifiers, and possibly conformance to a base UTI. Saving the DocumentSpecify the file formats your document can save to. Xcode fills in a If your app saves or exports to types that aren't in
Xcode includes a
In the function you must convert your document's contents to a Loading the DocumentThe
Read the file contents into a Next StepsThe following articles have more information on making document-based SwiftUI apps:
I have a WikiDemo app on GitHub as an example of a multi-platform, document-based SwiftUI app with a master-detail interface. |
Subscribe and get exclusive articles on Swift development, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.
Find the Code Causing Long SwiftUI Updates with Instruments Instruments comes with a SwiftUI instrument to find performance problems in your SwiftUI apps. This article shows how to use Instruments to find the code that causes long SwiftUI view updates. If you have never used Instruments, read the following article to learn how to profile your app with the SwiftUI instrument: Find the SwiftUI Views that Update the Most Using Instruments Finding Long Updates When you finish profiling your app,...
When you profile your app with the Allocations instrument, you may want to find the largest memory allocations your app makes. Take the following steps to find the largest memory allocations: Press Cmd-3 to open the allocations list. Click the Size column heading to sort the allocations by size. Choose All Heap Allocations from the Allocation Type menu in the bottom bar to hide virtual memory allocations. Your code doesn’t directly make virtual memory allocations. The Allocation Type menu is...
A common problem people run into when they start profiling their apps with Instruments is finding the code that is causing problems. Many instruments initially show general statistics instead of statistics about the code you wrote. For example the Allocations instrument initially shows the number of memory allocations and amount of allocated memory for hundreds of memory categories. If your app allocates a lot of memory, you want to find the code that allocates high amounts of memory. How do...