Skip to main content

NSDocument initWithType in Swift 2

·1 min

I updated my document-based Cocoa application to Swift 2 in Xcode 7 using Xcode’s converter. The converter changed the initWithType method in my NSDocument subclass to the following:

convenience init(type typeName: (String!)) {
    self.init()
    // Rest of initialization code here
}

After doing the conversion the Swift compiler crashed every time I built the project. I traced the problem to the initWithType method. I struggled to find a solution as an Internet and Stack Overflow search uncovered no one who had a similar problem. I asked about this on Apple’s paid developer forums, and user LCS provided the solution, which I will share with you.

convenience init(type typeName: String) throws {
    self.init()
    // Rest of initialization code here
}