Search code examples
iosxcodeuitableviewxcode6gm

Switched to xcode 6 GM - suddenly controller does not conform to UITableViewDataSource anymore


Today I updated to the newly released xCode 6 GM. When I try to build my app now I get a compiler error saying one of my View Controllers which handles a Table "does not conform to protocol UITableViewDataSource". Everything worked fine before (working with xCode 6 Beta 5)

According to the documentation the protocol requires 2 methods:

– tableView:cellForRowAtIndexPath: required method

– tableView:numberOfRowsInSection: required method

Both of them are implemented:

 func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return feed!.count
 }

 func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        println("HashtagDetailViewController: tableView - cellForRowAtIndexPath called")

        let cell = tableView.dequeueReusableCellWithIdentifier("postCell", forIndexPath: indexPath) as PostTableCell
        cell.loadItem(feed!.toArray()[indexPath.row])
        return cell
 }

Did I miss something? Is there a new required method that is not in the docs? Thanks for any help


Solution

  • Many Swift signatures have changed in use of optionals in the latest beta. So your existing implementation becomes an overloaded local method, rather than be the implementation of the protocol method. If it is a required method in the protocol, you will get these messages as the compiler assumes you have not implemented the required methods.

    Here it is a minor annoyance, but it is far more dangerous if you have implementations for optional methods in such protocols that have changed their signature. You will not get any compiler warnings and the methods you thought implemented the protocol methods will never get called.

    You have to check each of any such existing implementations manually to ensure, that above does not happen to your code.

    The simplest way to do this is to place your cursor in each of such method headers as you would normally do get quick help in the side window for the method. if it is correctly typed as a protocol method, you will see the description of the method in Quick help with the framework it is defined in at the end. If it has become a local method because of signature discrepancy, you will not see any description in Quick Help window but a listing of related methods and more telling the line which says it is defined in that local file.

    I don't know of any way to prevent checking every such existing optional implementation in your code manually after framwork changes like this.

    The protocol has changed slightly:

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
    

    is now:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell