Re: Category errata in Objective-C
I've had to do some funky things with categories in the past, in at least one case because some framework had added one but for complex reasons could not import. So, similar to what Jens said: The interface description of the category is for the sole purpose of helping the compiler generate a selector. This has the following implications: - you can put the interface description anywhere. You can put it at the top of one or more using implementation files, as well as the file that actually has the implementation for the category. Note: you do NOT need a implementation anywhere. If you ever had a project that failed to include the category implementation file, you know it will build and run, then crash when some code tries to use that category. [Ask me how I know this :-) ] - suppose you find a category in some framework used by your app, and want to use it. Or, you may want to know about it so you don't declare a similar method in your App, then have either the framework or your code crash because only one of those categories actually is installed. BIG REQUEST: if you write frameworks or libraries, don't use categories on anything but your own classes!!! Or, if you must, then namespace them with some whacky prefix so an app that defines a similar code doesn't crash! (Ask me how I know this :-) ) ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Category errata in Objective-C
Just for clarification, an empty name in the parens is referred to as an extension and not a nameless category. An extension has extra abilities such as adding properties. Just being explicit for those that may not know. Please excuse mobile typos > On Mar 29, 2019, at 5:55 PM, Jens Alfke wrote: > > > >> On Mar 29, 2019, at 2:51 PM, Alex Zavatone wrote: >> >> Also, I seem to remember the esteemed Jens stating that as soon as a >> category class is in your project, it applies to all objects instantiated >> from the base class and we only had to do an import if we wanted to use the >> category methods in the debugger. Please correct me if I am wrong here. > > No, you have to #import the header if you want to use the category methods in > a source file. > > At _compile_ time, when you call one of those methods the compiler needs to > have seen a prior declaration of that method or it'll flag an error. Thus, > you need to have #included the header file declaring the category. (The > compiler _could_ go ahead and decide you know what's right, and just compile > a call to that method anyway; back in the old days it used to, but that > turned out to lead to lots of stupid runtime crashes caused by mistyped > method names.) > > At _runtime_, the category patches the class implementation when your app > starts, so its methods are available everywhere. > > —Jens > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > https://lists.apple.com/mailman/options/cocoa-dev/cody%40servalsoft.com > > This email sent to c...@servalsoft.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Category errata in Objective-C
> On Mar 29, 2019, at 2:51 PM, Alex Zavatone wrote: > > Also, I seem to remember the esteemed Jens stating that as soon as a category > class is in your project, it applies to all objects instantiated from the > base class and we only had to do an import if we wanted to use the category > methods in the debugger. Please correct me if I am wrong here. No, you have to #import the header if you want to use the category methods in a source file. At _compile_ time, when you call one of those methods the compiler needs to have seen a prior declaration of that method or it'll flag an error. Thus, you need to have #included the header file declaring the category. (The compiler _could_ go ahead and decide you know what's right, and just compile a call to that method anyway; back in the old days it used to, but that turned out to lead to lots of stupid runtime crashes caused by mistyped method names.) At _runtime_, the category patches the class implementation when your app starts, so its methods are available everywhere. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Category errata in Objective-C
> On 29 Mar 2019, at 2:51 pm, Alex Zavatone wrote: > > In all the docs on categories, I seem to have missed what the term for the > text within the parens of a category declaration is called and what function > it serves. It's the category name. NSObject(Foo) represents "the Foo category on NSObject". As for what function it serves, well, that's a good question. I've never been aware of a runtime method for extracting such information or testing its presence (as one can for protocol conformation, for example). > Also, I seem to remember the esteemed Jens stating that as soon as a category > class is in your project, it applies to all objects instantiated from the > base class Categories are like a benevolent disease: once linked and loaded, they live everywhere. > and we only had to do an import if we wanted to use the category methods in > the debugger. Please correct me if I am wrong here. > > But I found out that that is not true. You have to import the category > header into any classes where you want to take advantage of the category > methods you added to a specific class. These amount to the same thing, which is exposing the symbol names to your interpreter or compiler for your convenient use as a programmer. -ben ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Category errata in Objective-C
In all the docs on categories, I seem to have missed what the term for the text within the parens of a category declaration is called and what function it serves. Any idea? Also, I seem to remember the esteemed Jens stating that as soon as a category class is in your project, it applies to all objects instantiated from the base class and we only had to do an import if we wanted to use the category methods in the debugger. Please correct me if I am wrong here. But I found out that that is not true. You have to import the category header into any classes where you want to take advantage of the category methods you added to a specific class. The import can be done on a specific file level, into a .pch if you want your whole app/module to be able to take advantage of the methods. Did I misread Jens’s statement? Cheers, Alex Zavatone Sent from my iPad ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com