• 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 04:15:14 +0200

PGM,

On 7.4.2006, at 4:02, PGM wrote:

I was kind of suprised that nobody yet commented

It was so much wrong nobody considered that important :) (namely since his "methodReturningAnNSString" was not a method returning a string).


on fact that d2kagw makes a comparison to @"NSCFArray", like:

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

I would think that this is wrong

It is, but for a few very special cases.

as I learned that the @".." construction returns a pointer to a newly allocated and autoreleased NSString.

You learnt wrong. Quite the contrary, it returns a (pointer to) a static string, allocated compile-time.


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).

Quite.

I made a little test to see whether I was correct:

    NSString *myString = @"aString";
    if(myString == @"aString"){
        NSLog(@"Match");
    }
    else{
        NSLog(@"No match");
    }

Running this code actually returns "Match", even though I would think the two instances of @"aString" would be independent.

They are not, for the reason above: @"..." is a static instance.

Yet if I do it another way:

NSString *anotherString = [[[NSString alloc] initWithString:@"bString"] autorelease];
if(anotherString == @"bString"){
NSLog(@"Match");
}
else{
NSLog(@"No match");
}


This return "No match". Can anybody shed a light on this?

This time you are getting your "newly allocated and autoreleased NSString".
---
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: 
 >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: Check the class of a variable?
  • Previous by thread: Re: Check the class of a variable?
  • Next by thread: Re: Check the class of a variable?
  • Index(es):
    • Date
    • Thread