Overriding the description method in NSObject to fast

I get one compiler error when I try to create one object in my xcode project. This is the code:

import UIKit class Rectangulo: NSObject { var ladoA : Int var ladoB : Int var area: Int { get { return ladoA*ladoB } } init (ladoA:Int,ladoB:Int) { self.ladoA = ladoA self.ladoB = ladoB } func description() -> NSString { return "El area es \(area)" } } 

Error during compilation:

 Rectangulo.swift:26:10: Method 'description()' with Objective-C selector 'description' conflicts with getter for 'description' from superclass 'NSObject' with the same Objective-C selector 

What do I need to do to override this function without problems?

+6
source share
1 answer
  • description is a (computed) property of NSObjectProtocol , not a method.
  • The Swift property returns a String , not an NSString .
  • Since you are overriding a superclass property, you must explicitly specify override .

Together:

 // main.swift: import Foundation class Rectangulo: NSObject { var ladoA : Int var ladoB : Int var area: Int { get { return ladoA*ladoB } } init (ladoA:Int,ladoB:Int) { self.ladoA = ladoA self.ladoB = ladoB } override var description : String { return "El area es \(area)" } } let r = Rectangulo(ladoA: 2, ladoB: 3) print(r) // El area es 6 
+12
source

Source: https://habr.com/ru/post/989480/


All Articles