7

How to create an NSProxy subclass in Swift?

Trying to add any of the init methods fails with error: "Super init can't be called outside of the initializer", or "Super init isn't called on all paths before returning from initializer"

error1 error2

Using Objective-C subclass as a Base class works, but feels more like a hack:

// Create a base class to use instead of `NSProxy`
@interface WorkingProxyBaseClass : NSProxy
- (instancetype)init;
@end

@implementation WorkingProxyBaseClass
- (instancetype)init
{
  if (self) {

  }
  return self;
}
@end



// Use the newly created Base class to inherit from in Swift
import Foundation

class TestProxy: WorkingProxyBaseClass {
  override init() {
    super.init()
  }
}
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115

1 Answers1

0

NSProxy is a abstract class. Apple docs about NSProxy says "An abstract superclass defining an API for objects that act as stand-ins for other objects or for objects that don’t exist yet".

The docs about abstract class of wikipedia says:

In a language that supports inheritance, an abstract class, or abstract base class (ABC), is a class that cannot be instantiated because it is either labeled as abstract or it simply specifies abstract methods (or virtual methods).

Calling super.init() for abstract class is wrong. In second class you are not calling super.init() for abstract class but of WorkingProxyBaseClass which is a concrete class. In Objective c you have not called [super init] hence code is working.

Aksen P
  • 4,564
  • 3
  • 14
  • 27
Abhiraj Kumar
  • 160
  • 1
  • 6