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"
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()
}
}
Simple solution: make Objective-C class that inherits from NSProxy and then inherit Swift class from it.
Example at my GitHub: https://gist.github.com/SoundBlaster/0e53bae4ab814537c5868564d95eb553
Don't forget about Objective-C Bridging Headers if you will mix languages in one project: https://developer.apple.com/documentation/swift/importing-objective-c-into-swift.