• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Checking the type...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Checking the type...


  • Subject: Re: Checking the type...
  • From: "Kenneth C. Dyke" <email@hidden>
  • Date: Thu, 13 Mar 2003 21:11:31 -0800

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.

  • Follow-Ups:
    • Re: Checking the type...
      • From: Jeff Disher <email@hidden>
References: 
 >Re: Checking the type... (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Odd crash with a drawer
  • Next by Date: Re: Stripping HTML tags
  • Previous by thread: Re: Checking the type...
  • Next by thread: Re: Checking the type...
  • Index(es):
    • Date
    • Thread