Craig Otis
Craig Otis

Reputation: 32054

Override multiple overloaded init() methods in Swift

I'm attempting to create a custom NSTextFieldCell subclass in Swift (Xcode Beta 5), with the following code:

class CustomHighlightTextFieldCell : NSTextFieldCell {

    required init(coder aCoder: NSCoder!) {
        super.init(coder: aCoder)
    }

    init(imageCell anImage: NSImage!) {
        super.init(imageCell: anImage)
    }

    init(textCell aString: String!) {
        super.init(textCell: aString)
    }
}

However, I receive compilation errors on the 2nd and 3rd init() declarations:

/Users/Craig/projects/.../CustomHighlightTextFieldCell:8:40: Invalid redeclaration of 'init(imageCell:)'
/Users/Craig/projects/.../CustomHighlightTextFieldCell.swift:17:5: 'init(imageCell:)' previously declared here

/Users/Craig/projects/.../CustomHighlightTextFieldCell:7:39: Invalid redeclaration of 'init(textCell:)'
/Users/Craig/projects/.../CustomHighlightTextFieldCell.swift:21:5: 'init(textCell:)' previously declared here

While there are some strange compiler bugs here (I'm getting the usual "SourceKitService Terminated, Editor functionality temporarily limited." message), it seems like I'm missing something in my method overriding - but I can't tell what.

I was under the assumption the named parameters, or at least the parameter types, would indicate that there are three different init() methods here, but apparently I'm missing a key piece of the puzzle.

Edit: If I add override to the 2nd and 3rd init() methods, I have a separate issue:

required init(coder aCoder: NSCoder!) {
    super.init(coder: aCoder)
}

override init(imageCell anImage: NSImage!) {
    super.init(imageCell: anImage)
}

override init(textCell aString: String!) {
    super.init(textCell: aString)
}

This new issue (simply invoked by adding the two override keywords) seems to almost contradict the original.

/Users/Craig/projects/.../CustomHighlightTextFieldCell.swift:17:14: Initializer does not override a designated initializer from its superclass
/Users/Craig/projects/.../CustomHighlightTextFieldCell.swift:21:14: Initializer does not override a designated initializer from its superclass

Upvotes: 4

Views: 3942

Answers (2)

Grimxn
Grimxn

Reputation: 22487

It would seem that your declarations (with override) should be sufficient, however, they seem to need the @objc declarations as well. This works:

class CustomHighlightTextFieldCell : NSTextFieldCell {

    required init(coder aCoder: NSCoder!) {
        super.init(coder: aCoder)
    }

    @objc(initImageCell:)
    override init(imageCell anImage: NSImage!) {
        super.init(imageCell: anImage)
    }

    @objc(initTextCell:)
    override init(textCell aString: String!) {
        super.init(textCell: aString)
    }
}

Upvotes: 5

ZYiOS
ZYiOS

Reputation: 5280

As you see, you have the corresponding super.init(xxx) in the functions which means you are overriding the functions. So just add the override keyword.

override init(imageCell anImage: NSImage!) {
    super.init(imageCell: anImage)
}

override init(textCell aString: String!) {
    super.init(textCell: aString)
}

Upvotes: 3

Related Questions