0

Possible Duplicate:
Comparing Strings in Cocoa

I need to perform logic on an NSString* variable.

If I do the following it works fine:

my_label_3.text = local_db_user.lifetime_subscription;

The label in the UI gets populated with the text, True

But the following returns NO:

local_db_user.lifetime_subscription == @"True"     

What code do I use for this pseudo code:

if local_db_user.lifetime_subscription == True
    do this
else
    do this other thing

My watch, during debugging, on local_db_user.lifetime_subscription shows:

{NSString * | 0x764e210} "True"
Community
  • 1
  • 1
pdschuller
  • 584
  • 6
  • 26

3 Answers3

1
if([local_db_user.lifetime_subscription isEqualToString:@"True"])
 {
        //do the task here
 }
 else
{

}
Rose
  • 437
  • 2
  • 9
0

You should use isEqualToString: to compare between to NSString objects.

As the Apple doc points out:

- (BOOL)isEqualToString:(NSString *)aString

Returns a Boolean value that indicates whether a given string is equal to the receiver using a literal Unicode-based comparison.

so to answer your question, your code should look like:

if ([local_db_user.lifetime_subscription isEqualToString:@"True"]) {
    // is equal
}
else {
    // is different
}
lmt
  • 811
  • 1
  • 11
  • 27
  • Thanks all. I see that people found this to be an exact duplicate. Well I hope it remains visible, because, after a lot of searching, I never found my answer in stack. - which I love. :-) Again thanks. – pdschuller Nov 23 '12 at 13:59
  • Hey @pdschuller, your welcome! As you can see, we are happy to help! Good Luck – lmt Nov 23 '12 at 14:56
0

Comparing object using equal operator(==) it's check for memory address. so compare the NSString object you have to use

  • (BOOL)isEqualToString:(NSString *)aString;

method.
for your own (custom class) objects have to override below method.

  • (BOOL)isEqual:(id)anObject
damithH
  • 5,148
  • 2
  • 27
  • 31