4 ms·
Objective-C categories are a land mine. 1) You have no way of knowing that your category method isn't going to collide with another method. That method could b
by swolchok 11y ago
Objective-C categories are a land mine.
1) You have no way of knowing that your category method isn't going to collide with another method. That method could be one of Apple's private ones, or it could be a method from a category used by another library you included in your app, or it could be even be from a category used in a jailbreak tweak. In all these cases, it's entirely possible that the colliding method doesn't have the same size arguments or return type as yours, in which case you are going to get extremely difficult to debug crashes. You also don't have guarantees around whose categories win.
2) As a class developer, you have no way of preventing categories from messing up internal guarantees of your class. Want it to be immutable? Too bad, some developer you've never talked to added methods in a category that require local state. Want to add and use a new private method? Too bad, that name is already taken by some random category, and now it looks like your code is breaking everything because it was just committed.
Prefixing category methods is a partial fix, but lots of folks aren't aware that they should do that.
- LinaLauneBaer 11y agoCollisions are very very rare. In over 10 years of Objective-C development I have maybe seen it once. Also: clang got much better at detecting collisions at compile time (where possible). That said: Swift extensions are safer because private categories cannot collide.
- swolchok 11y agoTry OBJC_PRINT_REPLACED_METHODS sometime. It's frightening. (http://stackoverflow.com/questions/9466732/in-objective-c-how-to-prevent-name-collision-in-category-methods http://stackoverflow.com/questions/9466732/in-objective-c-ho...)
- spiralpolitik 11y agoThis has been my experience as well.