Pergunta

I'm trying to add a property to UIBezierPath. So I created a subclass called JWBezierPath. I wanted to have all the class method shorthands also with number parameter, so I created the methods:

+ (JWBezierPath *)bezierPathWithColor:(UIColor *)color {
    JWBezierPath *path = [self bezierPath];
    [path setColor:color];
    return path;
}

The problem is, that [self bezierPath] does return an instance of UIBezierPath instead of my subclass. I also tried using concrete class: [JWBezierPath bezierPath]

How can I solve this issue?

Foi útil?

Solução

If you do not implement +bezierPath method in your class then superclass implementation will be called that will create instance of UIBezierPath, not JWBezierPath.

In theory it is possible for factory methods in base class to create instances even if those methods won't be overridden, e.g. consider 2 options for factory method in BaseClass:

// Will create instance of child class even if method won't be overriden
+ (instancetype) someObject {
   return [self new];
}

vs

// Will always create instance of base class
+ (BaseClass*) someObject {
   return [BaseClass new];
} 

However considering +bezierPath declaration (+(UIBezierPath *)bezierPath) and your evidence UIBezierPath class is not implemented that way and you have to implement +bezierPath method in your cub class

Outras dicas

bezierPath is defined as:

+ (UIBezierPath *)bezierPath

so you might want to use:

JWBezierPath *path = [[self alloc] init];

instead.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top