kevindeleon
kevindeleon

Reputation: 1924

Why does Xcode/Swift insist that I need to use the override keyword for Delegate and Data Source methods?

The following code works absolutely fine if I use the override keyword as Xcode insists I do...but if I remove the override keyword (as seen below in the Data Source extension), I get build errors. I see people organizing their code in this exact same way all over the place, and yet they aren't using the override keyword...any help? I mean, obviously if I just add the override keyword, everything is fine, but I'd like to know "why?". Is this something new in Swift 1.2 and I'm just seeing a bunch of old code?

import UIKit

class EventsTableViewController: UITableViewController {

    var events = [Event]()

    override func viewDidLoad() {
        super.viewDidLoad()

        var newEvent = Event(id: 1, type: "Birthday Party", city: "Dallas", state: "TX", date: "Saturday, July 12, 2015", time: "6:00PM - 11:00PM", details: "Some really cool stuff! Sed posuere consectetur est at lobortis. Maecenas faucibus mollis interdum. Maecenas sed diam eget risus varius blandit sit amet non magna. Nulla vitae elit libero, a pharetra augue. Etiam porta sem malesuada magna mollis euismod.")

        events.append(newEvent)

        newEvent = Event(id: 2, type: "Christmas Party", city: "Houston", state: "TX", date: "Friday, December 11, 2015", time: "6:00PM - 11:00PM", details: "Santa Clause and what not")

        events.append(newEvent)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // cast a var as destination View Controller
        let eventDetail = segue.destinationViewController as! EventDetialViewController

        // Pass the selected event over
        if let indexPath = tableView.indexPathForSelectedRow() {
            let selectedEvent = events[indexPath.row]
            eventDetail.currentEvent = selectedEvent
        }
    }

}

// MARK: - Table view data source
extension EventsTableViewController: UITableViewDataSource {

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("eventCell", forIndexPath: indexPath) as! UITableViewCell

        let currentEvent = events[indexPath.row]

        cell.textLabel?.text = "\(currentEvent.type) - \(currentEvent.date)"

        return cell
    }
}

Upvotes: 1

Views: 459

Answers (2)

Mike Taverne
Mike Taverne

Reputation: 9352

If your view controller inherits from UITableViewController, you need to use the override keyword because Apple already implements these methods in UITableViewController and you are overriding them.

If your view controller inherits from UIViewController, and conforms to the UITableViewDataSource and UITableViewDelegate protocols, then you do not need the override keyword since you are providing the first implementation of the protocol methods.

Upvotes: 6

gnasher729
gnasher729

Reputation: 52602

It insists on this to make sure that you know and the compiler knows that you are not writing a new function, but overriding an existing one. In Objective-C, if you spell the method name wrong, there is nothing the compiler can do to figure it out; you just wrote a method that isn't going to be called by anyone. In Swift, the compiler can tell you either "you used override, but this func isn't overriding anything", or "you are not using override, but your func uses the same name as another function".

Upvotes: 0

Related Questions