• 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: Check the class of a variable?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Check the class of a variable?


  • Subject: Re: Check the class of a variable?
  • From: Ondra Cada <email@hidden>
  • Date: Fri, 7 Apr 2006 00:10:33 +0200

James,

As an aside, you *may* have difficulty testing for class membership for primitives like NSArray or NSString. Many of these core classes are implemented as class clusters in the Cocoa framework, so even if you call [NSString stringWithBlah:...] you are not guaranteed to get back a subclass of NSString -- even if the returned object acts, smells, and quacks exactly like an NSString.

Ammm... not quite. What you get from stringWithBlah is *an* NSString. That is, all those NSCFStrings and what you call them are NSString subclasses.


(Of course, Apple could -- in principle -- return, say, an NSWindow from stringWithBlah if they wanted to. I'd say they would never ever want to do something that crazy, but seeing Boot Camp... who knows what comes next?)

On the other hand...

As an example, you cannot use isKindOfClass or isMemberOfClass to distinguish betweeen an NSArray and an NSMutableArray (search the Cocoa-Dev archives for the threads), even though the documentation would have you believe that NSMutableArray is a simple subclass of NSArray.

Strictly speaking, you can very dependably use isKindOfClass/ isMemberOfClass to distinguish betweeen an NSArray and an NSMutableArray. The catch is, you (almost) never see one: in practice ugly tollbridged stuff like NSCFArray occurs, and (from ObjC POV) it's a plain (non-direct either) NSMutableArray subclass -- which gets, alas, quite inconsistenly, returned from arrayWith... regardless whether you send it to NSArray or NSMutableArray.


Technically, it is all right and it does conform to documentation.

It is also inconvenient as hell if you need to distinguish between a mutable and an immutable container (and therefore in my personal opinion Apple should never have used this trick), but that's another story.
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Re: Check the class of a variable? (From: James Bucanek <email@hidden>)

  • Prev by Date: Replacing runModalSavePanelForSaveOperation and getting it right someday...
  • Next by Date: Re: NSThread close (exit)
  • Previous by thread: Re: Check the class of a variable?
  • Next by thread: Re: Check the class of a variable?
  • Index(es):
    • Date
    • Thread