Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

370
Vistas
Overriding non-@objc declarations from extensions is not supported

Seemingly out of nowhere I started getting hundreds of errors associated with two extension functions that I use frequently.

I tried commenting out all the new code that preceding this error showing up. I also tried cleaning my build folder. How do I get ride of the error found in the title of this post? Why did it randomly appear when I've been using these extensions successfully for months?

extension UITableViewCell {

public func getSize(large: CGFloat, medium: CGFloat, small: CGFloat) -> CGFloat {
    var size = CGFloat()
    let screenHeight = Int(UIScreen.main.bounds.height)
    if screenHeight >= 800 {
        size = large
    } else if screenHeight >= 600 {
        size = medium
    } else {
        size = small
    }
    return size
}

public func formatPrice(_ price: Int) -> String {
    let lastDigit = price % 10
    var stringPrice = ""
    if lastDigit == 0 {
        stringPrice = "$\(Double(price) / 100)0"
    } else {
        stringPrice = "$\(Double(price) / 100)"
    }
    return stringPrice
}

}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you add functions to a class in an extension (ie they are not in the class definition) then those methods are statically dispatched in swift because the compiler cannot add the methods to the class's vtable (ie the vtable needs to be made at compile time, but you can add an extension later, such as in another module). This is why the compiler is complaining that you cannot override the methods (ie they are non virtual).

Objective C dispatch works differently, via the selector table and it can be modified even at run time. Thats why the compiler says use @objc and sure enough if you do this it will work.

EXAMPLE:

This does not compile because we are trying to override and dynamically dispatch a statically dispatched non virtual function in swift

extension UITableViewCell {
    func a() {
        print("UITableViewCell")
    }
}

class B: UITableViewCell {
    override func a() {
        print("B")
    }
}

let b = B()
print(b.a())

This works and it prints "B" because its using objc selector dispatch

import UIKit
import PlaygroundSupport

extension UITableViewCell {
    @objc func a() {
        print("UITableViewCell")
    }
}

class B: UITableViewCell {
    override func a() {
        print("B")
    }
}

let b = B()
print(b.a())
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda