I would like to inherit from the framework class, which has a factory method. How can I make the factory method return an object of my inherited class type?
This is all you need to do:
@interface MONImage : UIImage @end @implementation MONImage @end
Then:
MONImage * image = [MONImage imageNamed:name];
How can I write, say, a subclass of UIImage that imageNamed: will return an object of my subclass type?
+[UIImage imageNamed:] implementation wrote subclasses from this approach. Therefore, you will need to implement this method yourself.
Here's how to declare a factory method:
+ (instancetype)imageNamed:(NSString *)pName;
and how to implement it:
+ (instancetype)imageNamed:(NSString *)pName { MONImage * image = [[self alloc] initWithThisDesignatedInitializer:pName]; ^^^^ NOTE: self, not a concrete class ...set up image... return image; }
but they didnβt do this - +[UIImage imageNamed:] wrote subclasses and returns UIImage when writing MONImage * img = [MONImage imageNamed:pName]; . Sometimes this is done for a good reason. Some methods must have βfinalβ semantics. This often appears when your method can return several types, as in a class cluster. The language does not express "final" methods, but such a method should be documented at a minimum.
So come to this UIImage case:
@interface MONImage : UIImage + (instancetype)imageNamed:(NSString *)pName; @end @implementation MONImage + (instancetype)imageNamed:(NSString *)pName { UIImage * source = [UIImage imageNamed:pName]; CGImageRef cgImage = source.CGImage; if (cgImage) return [[self alloc] initWithCGImage:cgImage]; // try it another way return nil; } @end
Note that UIImage and CGImage are immutable. This should not result in a deep copy of the image data.
source share