ios - Class Extension vs Primary Interface vs Category -
i new realm of ios. coming java , android background facing few challenges while learning objective c.
my question: understand how above 3 different each other fail understand use cases in practice.
do need class extension every class private functions? use of category, when can extend cocoa/cocoa-touch class in interface , add custom functions? please provide example experience.
category adding methods class in runtime. far runtime concerned, methods implemented in class extension, methods available class itself. category in objective-c fancy name monkey patching in other programming languages c#. can read here.
with said, can create category uicolor
method if want every uicolor
have behaviour throughout module. isn't case subclassing. subclassed (theoretically speaking) uicolor
object behaviour since there distinct difference in type of object.
example:
uicolor
has built in methods give different colors; can call uicolor.greencolor()
green color; uicolor.blackcolor()
black color , on...
suppose want own called in similar fashion, create category (example in swift) so
extension uicolor { static func yourcolor() -> uicolor { return uicolor(red:220/225,green:222/225,blue:223/225) } }
this way, valid call uicolor.yourcolor()
. every uicolor
use has method available. convenient subclassing, isn't it?
creating subclass has polymorphic implications; categories don't. subclass when need refinement of existing class , treat both parent , child when required. java developer know when makes sense subclass.
an extension best private methods declare in .m
file. think of extension category private .m
file.
Comments
Post a Comment