Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Document class in Swift document-based application

I am building a new document-based application in Swift for OS X using Xcode. I would like the default Document class to be called something that is not Document (in this case Notebook), though of course it would still subclass NSDocument (or NSPersistentDocument if I use Core Data). I can rename its file from Document.swift to Notebook.swift, but once I rename the actual class from Document to Notebook, running the application pops up the error alert "No document could be created." The console tells me "The DocumentType type doesn't map to any NSDocumentClass."

So, obviously I have to do other things to make the document architecture recognize my class once I have renamed it. But what? I found some directions on another site but they are old and written for Objective-C and XIB when I am using Swift and storyboards; what I need to do is likely to be quite different. What does one need to do to rename the main document class for a document-based application, in Swift, using storyboards?

EDIT: There was a request below for code. Basically, the default Document.swift or Notebook.swift file looks like

import Cocoa

class Document: NSDocument {
... [standard code for Document class goes here, edited out for brevity]
}

and I changed it to

import Cocoa

class Notebook: NSDocument {
[...]
}

It worked with the version on the top and does not work with the version on the bottom.

like image 742
Displaced Hoser Avatar asked Feb 07 '23 18:02

Displaced Hoser


1 Answers

Willeke's suggestion above to search for "Document" (entire word, matching case) allowed me to find what I needed to do. For the record, I needed to go into Info.plist and go to Document types/Item 0(Document type)/Cocoa NSDocument Class , and change "$(PRODUCT_MODULE_NAME).Document" to "$(PRODUCT_MODULE_NAME).Notebook".

like image 199
Displaced Hoser Avatar answered Feb 24 '23 06:02

Displaced Hoser