No document can be created in Swift

I have the same problem that was reported in a previous thread on devforums.apple.com in thread/231762/

I have an existing document-based Mac app that I ported to Swift. The code compiles, but when I try to create a new document, an alert opens saying "No document can be created." The Xcode debug console has the following message:

The *** type doesn't map to any NSDocumentClass.

I have read the solution from Mark Szymczyk :

give the Product Module Name build setting a value. In the Info.plist file, enter the following for the Cocoa NSDocument Class field:

Module.Class

Where Module is the value you entered for the Product Module Name build setting and Class is the name of your NSDocument subclass.

I did it, I cleaned the build, but still have the problem ; I suspect I do not enter the right Module name.


What is exactly the module name to enter (where is it defined exactly ?) ; I have a project name with spaces ; I have replaced the space by hyphens " - " ; is it the right thing to do ?

Accepted Reply

According to the Using Swift with Cocoa and Objective-C documentation, any non-alphanumeric characters in the module name are replaced with an underscore.


The name of the Xcode-generated header for Swift code, and the name of the Objective-C bridging header that Xcode creates for you, are generated from your product module name. By default, your product module name is the same as your product name. However, if your product name has any nonalphanumeric characters, such as a period (.), they are replaced with an underscore (_) in your product module name. If the name begins with a number, the first number is replaced with an underscore.


You can also provide a custom name for the product module name and Xcode will use this when naming the bridging and generated headers. To do this, change the Product Module Name build setting.


NOTE: You cannot override the product module name of a framework.

Replies

According to the Using Swift with Cocoa and Objective-C documentation, any non-alphanumeric characters in the module name are replaced with an underscore.


The name of the Xcode-generated header for Swift code, and the name of the Objective-C bridging header that Xcode creates for you, are generated from your product module name. By default, your product module name is the same as your product name. However, if your product name has any nonalphanumeric characters, such as a period (.), they are replaced with an underscore (_) in your product module name. If the name begins with a number, the first number is replaced with an underscore.


You can also provide a custom name for the product module name and Xcode will use this when naming the bridging and generated headers. To do this, change the Product Module Name build setting.


NOTE: You cannot override the product module name of a framework.

I have read the solution from Mark Szymczyk :

An alternative would be to use

@objc(Foo)
on your document class to have it exported to Objective-C with the name
Foo
.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, I replaced dash ( - ) by underscore and it did improve: I can now create and save a document.


However, I get the following message (note: Lesson 20 Grades is the name of the application):

2015-11-25 12:01:57.714 Lesson 20 Grades[11143:4726300] -[NSDocumentController fileExtensionsFromType:] is deprecated, and does not work when passed a uniform type identifier (UTI). If the application didn't invoke it directly then the problem is probably that some other NSDocument or NSDocumentController method is getting confused by a UTI that's not actually declared anywhere. Maybe it should be declared in the UTExportedTypeDeclarations section of this app's Info.plist but is not. The alleged UTI in question is "com.mycompany.Lesson_20_Grades".

And the document that is created cannot be opened by the application. Is there a link with the above message ?

In my app info.plist, the Document OS Types is set to ???? Should I define it ?


PS: I looked at Lister example. In the info.plist, the Document Content Type UTIs is com.example.apple-samplecode.Lister (with a dash) ; how can it work ?

Hi,


when I add @obj(Document) in NSDocument Class definition, I get the following execution error message


2015-11-25 12:17:41.169 Lesson 20 Grades[11222:4817041] The com.mycompany.Lesson_20_Grades type doesn't map to any NSDocumentClass.

2015-11-25 12:17:41.170 Lesson 20 Grades[11222:4817041] -[NSDocumentController openUntitledDocumentOfType:andDisplay:error:] failed during state restoration. Here's the error:

Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened."

Two things:

  • Underscore is not valid in UTIs. You can find a list of valid characters in Uniform Type Identifiers Overview.

  • If, once you’ve fixed the above, you’re still having problems, please post your CFBundleDocumentTypes declaration.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I am having this exact problem now.

it makes no sense whatsoever. the deprecated method is not called by me, the clear, well written note has no information in it at all. My app, which used to work... stopped loading files suddenly (without recompiling btw, it just stopped working) and now It won't create files of the type I set in the project, or load any files of any kind.


this is a core data, document based, swift app. The offending code wasn't written by me. it's one of the free-of-charge features fo core data.

I am still having this problem.

document based mac app this time.


worked just fine a month or 2 ago. Now it will not save out any files.

error message:


-[NSDocumentController fileExtensionsFromType:] is deprecated, and does not work when passed a uniform type identifier (UTI). If the application didn't invoke it directly then the problem is probably that some other NSDocument or NSDocumentController method is getting confused by a UTI that's not actually declared anywhere. Maybe it should be declared in the UTExportedTypeDeclarations section of this app's Info.plist but is not. The alleged UTI in question is "com.bkTools.concreteDoc".


couple notes about that:

1. the UTI is declared. It's declared as a document type and as an exported UTI type.


2. all of the code I HAVE written in regards to saving files executes without complaint. I never once call : fileExtensionsFromType. It's Apple's code doing that.


I am not happy that code that worked, now just doesn't work. I'd love it if it were some simple fix, but frankly the entire NSCoding system is falling apart, and there's nothing coming to replace it.