"conflicts with previous declaration" in Swift extension

Hi guys.


I just finished the WWDC 2015 video "Improving Your Existing Apps with Swift" and I want to try out extending my existing classes with Swift. At around 25:16 of the WWDC video, there's this part where the speaker overrides a UITableViewDelegate method - tableView:accessoryButtonTappedForRowWithIndexPath: in his Swift file.


I tried to do the same thing to my extension, but with - tableView:heightForRowAtIndexPath:. The problem is I am getting a compiler error:


Method 'tableView(_:heightForRowAtIndexPath:)' with Objective-C selector 'tableView:heightForRowAtIndexPath:' conflicts with previous declaration with the same Objective-C selector


I did not implement this method in my Objective-C file so I don't know why it clashes. Tried it on the latest Xcode 7.0 Beta (7A152u) and Xcode 6.4. Got the same error.

Answered by allanrvj in 25984022

Thanks. I added the return type and a stub of returning 1.0, but it is still reporting the same compiler error.


Also, it's not a class but a Swift extension of an Objective-C class. As in:


import Foundation
extension TestTableViewController {

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 1.0
    }
}


Doesn't compile.


Edit: I know now. Insert a "public override". Now it compiles 🙂


import Foundation
extension TestTableViewController {

    public override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 1.0
    }
}


There is a fix-it suggestion on Xcode 7.0 which is not available in Xcode 6.4. Also, it doesn't compile on Xcode 6.4 but compiles on Xcode 7.0 Beta.

It sounds like your method has a different signature than the delegate method.


Does it look like this:

class TestDelegate: NSObject, UITableViewDelegate
{
    func tableView(tableView: UITableView, heightForRowAtIndexPath: NSIndexPath) -> CGFloat
    {
       
        return 1.0
    }
}
Accepted Answer

Thanks. I added the return type and a stub of returning 1.0, but it is still reporting the same compiler error.


Also, it's not a class but a Swift extension of an Objective-C class. As in:


import Foundation
extension TestTableViewController {

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 1.0
    }
}


Doesn't compile.


Edit: I know now. Insert a "public override". Now it compiles 🙂


import Foundation
extension TestTableViewController {

    public override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 1.0
    }
}


There is a fix-it suggestion on Xcode 7.0 which is not available in Xcode 6.4. Also, it doesn't compile on Xcode 6.4 but compiles on Xcode 7.0 Beta.

"conflicts with previous declaration" in Swift extension
 
 
Q