[SWIFT] What you need to do to open a file from the menu in the document-Based App macOS app

When I thought about making an image viewer, I was lightly addicted to it with my first move, so some people may stumble on the same thing. The environment is Xcode 12.3.

Create a new project in Document App

This is natural, so the explanation is omitted.

Register the target file type in Document Types

Register the file in Target-> Info-> Document Types.

For Class, specify the "Document" class that is automatically created when the project is created.

Specify UTI for Identifier. I can't find any information about Apple's home about what kind of character string to specify, but a list is given on the following site. http://potting.syuriken.jp/potting_conv/understanding_utis_J/chapter4.html

Below is an example for opening an image file.

スクリーンショット 2021-01-16 9.30.09.png

Make the Document class available from Objective-C

It seems that the "Document" class that inherits NSDocument cannot be used as it is in Swift-based apps, so it is necessary to add a line using @objc. (I couldn't find this information easily ...) Specifically, it looks like this.

@objc(Document) //add to
class Document: NSDocument {
...

Modify the method of loading the Document class

At this point, you can select the file, so add the actual reading process. By default, the following methods are called.

override func read(from data: Data, ofType typeName: String)

If you want to get the file path, override the following method.

override func read(from: URL, ofType: String)

Recommended Posts

What you need to do to open a file from the menu in the document-Based App macOS app
What you need to know before writing a test in an iOS app
What to do if you get a java.io.IOException in GlassFish
What to do if you can't find the JDK you put in SDKMAN after migrating from bash to zsh on macOS
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do if you get a gcc error in Docker
What to do if you get a DISPLAY error in gym.render ()
What to do if you forget the root password in CentOS7
[Output] Learn Ruby from what you want to do at the fastest speed-Part 2 "Create a flowchart"
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you can't install the plugin from the Eclipse marketplace
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do when you want to delete a migration file that is "NO FILE"
What to do if the changes are not reflected in the jar manifest file
Androd: What to do about "The Realm is already in a write transaction in"
What to do if you accidentally create a model
What to do when rails creates a 〇〇 2.rb file
What to do if you get a wrong number of arguments error in binding.pry
What to do if you can't get the text of an element in Selenium
What to do if you don't see the test code error message in the terminal console
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
[IOS] What you need to know before creating a widget
What to do if you change the Listen Address from the settings screen with Mattermost docker
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
[Maven] What to do if you are asked to incorporate a jar that is not in the remote repository into the war
What to do if you get a "302" error in your controller unit test code in Rails
What to do if you can't use the rails command
What to do if you select a JRE in Eclipse and get "The selected JRE does not support the current compliance level 11"
I tried to explain what you can do in a popular language for web development from a beginner's point of view.
What do you need after all to create a web application using java? Explain the mechanism and what is necessary for learning
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
What to do when you want to know the source position where the method is defined in binding.pry
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do about the "cannot be read or is not a valid ZIP file" error
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
What to do when the changes in the Servlet are not reflected
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
Add a time stamp to the JAR file name in Gradle
How to open a script file from Ubuntu with VS code
What to do when you become a Problem During Content Assist
What to do when you run into a docker-compose node_modules problem
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
If you leave the Next.js app open in Chrome, you will get a "/ _next / webpack-hmr" Routing error in the Rails app.
What to do if the image posted by refile disappears after setting a 404 error page in Rails
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
[Output] Learn Ruby from what you want to do at the fastest speed ・ Part 1 "Think about what you want to make"
What to do if the prefix c is not bound in JSP
How to run a GIF file from the Linux command line (Ubuntu)
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
What to do if you get a MiniMagick vulnerability alert on GitHub
Notes on what to do when a WebView ClassNotFoundException occurs in JavaFX 12
What to do if you are told "there is no main manifest attribute" when creating a jar file containing dependencies in a maven project
What to do when a javax.el.PropertyNotWritableException occurs
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you have enabled Use the WSL2 based engine in Docker Desktop with insufficient WSL2 installation
What to do if you install Ubuntu
How to translate the error message into Japanese (What to do if you cannot log in for some reason)
What to do about "A server is already running ..." that happened without turning off the rails server in the terminal
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE