Re: Checking the type...
Re: Checking the type...
- Subject: Re: Checking the type...
- From: Jeff Disher <email@hidden>
- Date: Fri, 14 Mar 2003 02:12:05 -0500
Yes, this is, in my opinion the best approach. I did this when
creating an NSToolbarItem subclass. When asking for an array of
toolbar items, I didn't know if they implemented the method so I just
implemented it as a category of the NSToolbarItem class and then could
call the method knowing that it had defined behavior in all cases.
The only problem of course, is that you have to know which types of
objects you could be having to message and implement the category in
each one to do what you want but that shouldn't be an issue.
The other interesting thing about this is that it is something that
works very well in Objective-C. After implementing something like
this, you will be in love with the language forever.
Hate to be a "me too" but I figured that my example did a good job of
showing when this was useful,
Jeff.
On Friday, March 14, 2003, at 12:11 AM, Kenneth C. Dyke wrote:
On Thursday, March 13, 2003, at 07:51 PM, Nick Zitzmann wrote:
On Thursday, March 13, 2003, at 07:39 PM, Hussain Bandukwala wrote:
I have entries of various types (NSString, etc.) in an
NSMutableArray and was wondering if there is a way to check for
these types. I would like to perform different functions on the
entries if they are of the NSString type and different functions if
they are of some other type. Could someone please tell me what
method(s) to use for checking for the NSString type?
Use "-isKindOfClass" and "+class" to do this. For example, given a
class named "class":
if ([class isKindOfClass:[NSString class]] == YES)
{
// if your code gets to this point, then class is an NSString or
inherits from NSString
}
While this certainly would work, it would be far cleaner (IMHO) to
just implement category methods on the various classes you wish to
deal with to do whatever work it is you want to do. Then you can just
let the Objective C dispatcher figure it all out, without writing a
bunch of branchy code that potentially winds up making way more method
calls than you'd get by just using the method dispatcher to begin > with.
The code above (for example) takes two method calls to figure out
whether the object is a string or not, before you've actually done any
work. With a category method, you'd only ever take one method call.
-Ken
Kenneth Dyke, email@hidden (personal), email@hidden (work)
Sr. Mad Scientist, MacOS X OpenGL Group, Apple Computer, Inc.
Java: The blazing speed of SmallTalk with the simple elegance of C++.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
Jeff Disher
President and Lead Developer of Spectral Class
Spectral Class: Shedding Light on Innovation
http://www.spectralclass.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.