-5

My question may be simple for some to answer, but I'm relatively new to Objective C and Xcode. So I have a UILabel and I am running an if statement asking if UILabel is equal to self.NSString then do ... Here is the code.

if (UILabel.text == self.NSString)
{
//Do Something here...
}

I'm wondering if this would work, or what I have to do in order for this to start working. Thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

5

Use isEqualToString: method from NSString class.

if([text isEqualToString:string])
{
  // Do something here.
}
TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
1

When comparing strings, you should use

[UILabel.text isEqualToString:self.NSString]

The == simply compares the pointers, which will often be different even if their contents are the same. The isEqualToString method compares the contents.

user1459524
  • 3,613
  • 4
  • 19
  • 28