NSPersistentDocument initWithType Crashes Compiler

I have an existing document-based Core Data application that I converted to Swift 2 in Xcode 7. After converting the project, the compiler crashes every time I build the project. I narrowed the problem down to the convenience init method I override in my NSPersistentDocument subclass. If I comment out the init method, the project builds.


But if I have the convenience init method in my code, the compiler crashes, even if the only thing the init method does is call self's init method.


convenience init(type typeName: (String!)) {
        self.init()
}


What changes do I have to make to the convenience init method to prevent the Swift compiler from crashing when building the project?

Answered by LCS in 22862022

It looks like that NSDocument convenience intializer was changed to be this:

convenience init(type typeName: String) throws


The Swift compiler may not have liked the combination of the explicitly unwrapped optional and the lack of the throws keyword?


But in a playground in Xcode 7 beta 2 I just see a warning about overriding with the change to an optional, so if you aren't using beta 2 or beta 3, you'll probably want to download a newer beta.

Removing type from the argument list eliminates the compiler crash. The following code compiles without crashes or compiler errors:


convenience init(typeName: String) {
        self.init()
}


But now the convenience init method is not called. How do I implement NSDocument's initWithType method without crashing the compiler?

Accepted Answer

It looks like that NSDocument convenience intializer was changed to be this:

convenience init(type typeName: String) throws


The Swift compiler may not have liked the combination of the explicitly unwrapped optional and the lack of the throws keyword?


But in a playground in Xcode 7 beta 2 I just see a warning about overriding with the change to an optional, so if you aren't using beta 2 or beta 3, you'll probably want to download a newer beta.

Your method signature works. Thanks.

NSPersistentDocument initWithType Crashes Compiler
 
 
Q