Adrian_H Adrian_H - 10 months ago 63
Swift Question

Overriding a property in a swift class which is defined initially in a Obj-c superclass

Although I fully appreciate it's bad practice for strongly typed implementations, I have a some generic code that needs to be changed in a subclass. The getters and setters in the obj-c code deals with id typed values so it is expecting more generalised values.

// objc
@interface A
@property NSNumber* foo;

// swift subclass
class B : A {
var foo: String?

Is there anyway to define a property is Swift so that it replaces entirely the superclass property?

The loosely typed nature of Obj-c with the (id) type allows some flexibility, that is closed down in swift.


This is not possible. You can override the property to provide different behavior, but changing the type violates OO design and thus doesn't compile.

To override a property:

override var foo: NSNumber! {
  get { return NSNumber(int: 5) }
  set { }