John Hass John Hass - 3 months ago 10
iOS Question

Swift: Override function

I am taking a iOS course online provided by a famous university. I don't understand why the following code use

override
and it is legal.


  1. According to the official definition, we use
    override
    to override superclass' methods. Where is the subclass and superclass in the following code?

  2. What's been override and by what?




public override var description: String {
return "\(url.absoluteString) (aspect ratio = \(aspectRatio))"
}


Answer

According to the official definition, we use override to override superclass' methods.

That's correct. The superclass in your example is the class that encloses the override of description property. This could be NSObject, some other class derived from it (directly or indirectly), or some class unrelated to NSObject that has var description: String property.

description is a property that Swift classes commonly have as a way to present themselves as a string, because description provides conformance to CustomStringConvertible protocol. This is similar to toString() method of Java, and to str() method of Python.

What's been override and by what?

The implementation of the property is what's being overridden. The class that has the implementation does the overriding.

Comments