I have an Objective-C subclass of 'CAGradientLayer' that overrides the '+ (CAGradientLayer *)layer' initializer. It is correctly included in my Bridging-Header
#import "ONCGradientLayer.h"
@implementation ONCGradientLayer
+ (ONCGradientLayer *)gradientLayer {
return [ONCGradientLayer layer];
}
+ (ONCGradientLayer *)layer {
ONCGradientLayer * layer = [super layer];
layer.colors = @[(id)[[UIColor colorWithWhite:0.0 alpha:1] CGColor], (id)[[UIColor colorWithWhite:0.1 alpha:1] CGColor]];
layer.startPoint = CGPointMake(0, 0);
layer.endPoint = CGPointMake(0, 1);
return layer;
}
This all works as expected from Objective-C, by calling either [ONCGradientLayer layer]
or [ONCGradientLayer gradientLayer]
When I try to call it from swift, I'm given a compiler warning that the method has been renamed init
, so I must call ONCGradientLayer()
This, however, does not work, and the overridden initializer is never called.
I can work around this by adding a differently named initializer :
+ (ONCGradientLayer *)swiftInit {
return [ONCGradientLayer layer];
}
I can successfully called that in swift using ONCGradientLayer.swiftInit()
What is going on that prevents me from calling ONCGradientLayer()
and getting the overridden initializer?
UPDATE :
I can make the ONCGradientLayer()
work by refactoring it to use the init
method, though I'm still curious why the other approach does not work.
- (instancetype)init {
if (self = [super init]) {
self.colors = @[(id)[DEFAULT_BACKGROUND_COLOR CGColor], (id)[[UIColor colorWithWhite:.1 alpha:1] CGColor]];
self.startPoint = CGPointMake(0, 0);
self.endPoint = CGPointMake(0, 1);
}
return self;
}