Design the Document Content with Multiple Document Type

My app that I’m developing in AppKit, use two type of file:

  • custom type with extension webcodeproj: is based on public.data and it is the type of the app. it is the file that is created when you create a new project and contains all information about the project.

  • text type file: the app starting with the possibility to create, read, edit and delete file of type text with extension HTML and CSS

The Apple Documentation about the creation of document-based app has an example project. It explains

This sample app is a text editor, implementing a subclass of NSDocument called Document with a data model called Content.

The Content object encapsulates the document’s data into a single package. Putting all of the document’s data into a single Content object simplifies data management. For example, it’s easier to add new data elements to the document later.

The Content object contains a single attribute called contentString, which is made up of a single String.

This is perfect for only one type and because the sample app is a text editor, is perfect for me. but I’ve two question:

  1. how can I handle also the custom type of files(in my case webcoreproj) that is not a text?
  2. both html and css are text file, how can I eventually manage the files separately?
    the methods
func data(ofType typeName: String) throws -> Data 
func read(from data: Data, ofType typeName: String) throws

has typeName as argument of method, I suppose that inside of method I have to make condition like

if typeName == "css" 

is it right? is it correct?

@robertomachorro @sarah

Hi @rufy . First, there is no need to page Sara or I on your messages, anybody in this Forum that can answer, will answer. Also, we all do this voluntarily, please be respectful of our time.

As for your question, the typeName actually refers to Uniform Type Identifier names. They look like this:

public.svg-image
com.apple.xml-property-list
public.objective-c-plus-plus-source
public.markdown
...

They are analogous to Content-Type in HTTP. And are meant to identify a file by its type.

  1. Uniform Type Identifier - Wikipedia
  2. Apple Developer Documentation

I’m sorry Roberto! I know that anybody in this Forum can answer. Anybody will answer? unfortunately I’m not so sure. And actually only you and Sarah has write the book “MacOS by Tutorials” and, for this reason, surely can answer to questions about MacOS. In fact the tutorials about macOS development are yours. I absolutely didn’t want to disrespect you or to Sarah regarding your time. and I did not expect such a prompt response. usually my questions after 6 months are closed without an answer or a really useful answer to solve the problem. and if this is true for swift / ios even more so for MacOS, where tutorials and updated documentation are really sparse. Unfortunately, the fact that the book only dealt with the subject for SwiftUI disappointed me a bit. that’s why I try to contact you as much as possible. I have this project that I would like to develop but, as I said, updated documentation and tutorials are very few. But I understand that you are busy. therefore, in the hope that you write articles for Appkit and receive sensible answers from users of this Forum, I will write the posts without putting your name.
Sorry

Hi @rufy

To answer your questions:

  1. You’ll set up a file type just like you did for HTML & CSS files, but using your own unique identifier. then you can save and read the file as Data. After. that, how you process it depends on your internal file structure.

  2. I would expect the typeName to be whatever identifier you used like “public.css” or “public.html” but its easy to test this by printing out the typeName.

1 Like