Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Checking Objective C

I have a UITextField that is supposed to contain a integer value. When I attempt to check weather the value that is placed in the UITextField is equal to a number it never works. Looking for solutions.

5 Answers

that number thats in the text field is a string object which makes things a bit tricky. if you want to see if its equal to a number you could use

    if ([self.textField.text isEqualToString:@"5"]) {

}

or convert the text in the text field to an integer:

    if ([self.textField.text intValue] == 5) {

}

That's great. I tried what you said, and thanks to you, it works great now. Thanks again.

By why did yo make it a == in the second example.

== is a comparison operator. You can use it to see if an integer is equal to another integer etc. It works on the primitive types and nil I believe (could be wrong on that). The reason you had to use the isEqualToString method on the first one is because strings are objects so using == wont work on them.

So in the first example I just compared a string to a string, so since strings are objects I had to use the isEqualToString method. In the second example I converted the string thats in the text field to an integer using the intValue method and then compared it to another integer using the comparison operator ==

I thought that too. So I tried it with one "=" and it still worked.

hmm. it shouldnt have. it should have been a syntax error and said that "expression is not assignable" or something along those lines

I'm wondering why too, but it still worked. Thanks for the help.