objective c - When are categories bad/dangerous? -
i'm using categories core data. of tutorials i've read , lectures i've listened categories considered "bad" practice. because objective-c dynamic seems okay define methods somewhere else, because public properties of class can used. pitfalls should looking out when using categories? or there reason categories bad practice? reason i'm using them core data don't have rewrite add-on methods every time regenerate subclasses.
the "danger" can think of when use them replace methods in original class rather subclassing.
when doing lose ability access original implementation, which, since private method overriding, have unforeseen effects.
using categories add extra methods object of particular class great, , precisely for. using them core data, doing, fine because allow change model , regenerate "vanilla" object without destroying code.
tip of hat @codafi bit of documentation apple:
although objective-c language allows use category override methods class inherits, or methods declared in class interface, discouraged doing so. category not substitute subclass. there several significant shortcomings using category override methods:
when category overrides inherited method, method in category can, usual, invoke inherited implementation via message super. however, if category overrides method exists in category's class, there no way invoke original implementation.
a category cannot reliably override methods declared in category of same class.
this issue of particular significance because many of cocoa classes implemented using categories. framework-defined method try override may have been implemented in category, , implementation takes precedence not defined.
the presence of category methods may cause behavior changes across frameworks. example, if override windowwillclose: delegate method in category on nsobject, window delegates in program respond using category method; behavior of instances of nswindow may change. categories add on framework class may cause mysterious changes in behavior , lead crashes.
Comments
Post a Comment