• 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: Andrew Farmer <email@hidden>
  • Date: Thu, 6 Apr 2006 19:16:05 -0700

On 06 Apr 06, at 19:02, PGM wrote:
I was kind of suprised that nobody yet commented on fact that d2kagw makes a comparison to @"NSCFArray", like:

if ( [myObject methodReturningAnNSString] == @"NSCFArray" )

I would think that this is wrong as I learned that the @".." construction returns a pointer to a newly allocated and autoreleased NSString.

Not quite... it returns a pointer to a statically allocated NSString. These may be shared within a module, so (@"x" == @"x") is always true; however, if f() is a function defined externally that returns @"x", (f() == x) may not be true.


This NSString does not necessarily have to be the same as the object you are comparing it to, even if they would have contained the same string (because of this I always use isEqualToString). I made a little test to see whether I was correct... Running this code actually returns "Match", even though I would think the two instances of @"aString" would be independent.

Yes, actually, it does. The == operator has NO introspection in Objective C. All it does is compare the literal values of two variables - not what they point to. The reason it appears to be doing a string comparison here is because the two instances are coalesced. If one is within a separate source file, though, it *may* not be equal.


Yet if I do it another way... this returns "No match". Can anybody shed a light on this?

This time, you're dynamically allocating a separate NSString object, which is separate from the static instance. This is necessarily not equal to the static string.

Attachment: PGP.sig
Description: This is a digitally signed message part

 _______________________________________________
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: 
 >Check the class of a variable? (From: d2kagw <email@hidden>)
 >Re: Check the class of a variable? (From: PGM <email@hidden>)

  • Prev by Date: Re: Check the class of a variable?
  • Next by Date: Re: bindings screwup
  • Previous by thread: Re: Check the class of a variable?
  • Next by thread: Programmatic web form fill-in?
  • Index(es):
    • Date
    • Thread