Cocoa programming issue

Joined
Sep 14, 2008
Messages
18
Reaction score
0
Points
1
I tried this same question on some other boards and its up to like 70 views and no replies :|

Okay. So I'm trying to make a simple practice application with Xcode and Interface Builder 3. As part, I want the user to enter text into a field and press a button which will check if the input matches something, and if it does, display correct. So here is the code that will do that when the button is pressed:

Code:
- (IBAction)checkPass:(id)sender {

  if ([textField stringValue] == @"fourthdimension") {
  
     [infoLabel setStringValue:@"Your password was correct!"];
	 
  } else {
  
     [infoLabel setStringValue:@"Your password was incorrect!"];
	 
  }
  
}

Now, after trying this with the rest of the app, it gives me incorrect for everything I put in, including the right answer, "fourthdimension." I have no idea why it's doing this. To test it, I tried the whole same concept with integers:

Code:
- (IBAction)checkPass:(id)sender {

  if ([textField intValue] == 5) {
  
     [infoLabel setIntValue:1];
	 
  } else {
  
     [infoLabel setIntValue:0];
	 
  }

}

This, for some reason, works. When I input 5, it gives me 1. For everything else, 0. This is correct.

I think it might somehow be space thats causing an entered string not to equal @"fourthdimension". Could it be taking all the rest of the space in the text field? Is there any type of string class that ignores spaces or something? Or the problem could be something else...

I didn't exactly know where to put this question on the forums, so I just put it here. Hope somebody can help.
 

rman


Retired Staff
Joined
Dec 24, 2002
Messages
12,637
Reaction score
168
Points
63
Location
Los Angeles, California
Your Mac's Specs
14in MacBook Pro M1 Max 32GB 2TB
note: moved to a better forum
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
I think you want to use the compare: method of NSString for your comparison.

See Apples NSString documentation. To know what gets returned, you actually have to read the part of the document for method:
Code:
compare:options:range:locale:
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
You can't use the equality operator for this test. All it is doing is testing the pointers to the two NSString objects for equality. The same code works with the integer version of the test because integers are simple variables for which the equality operator can be used.

(Edit: xstep beat me to it. :) )
 
OP
M
Joined
Sep 14, 2008
Messages
18
Reaction score
0
Points
1
Thanks, I understand the concept, and I found the "isEqualToString" method. According to the documentation, it returns a BOOL value:
Code:
- (BOOL)isEqualToString:(NSString *)aString

So I tried this code to begin my if:
Code:
if ([textField isEqualToString:@"fourthdimension"] == YES) {

Now, however, it seems like it doesn't even execute the if. It's got to be this statement that begins the if because I replaced just this line with simpler if with integers and it worked. After I press the button and execute checkPass:, the label just stays at its awakeFromNib value: "Enter password".

Sorry, but any help???
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Try removing "== YES" although I'm not sure it should matter.

Also, is textField defined as an NSString, or somthing else?
 
OP
M
Joined
Sep 14, 2008
Messages
18
Reaction score
0
Points
1
Removing the == YES didn't change anything, but didn't make it work either. It's weird because it's like stopping at the if statement. Any code I add before if still works. I don't know what could be causing that. I have no warnings or errors either. It's only if I use:

Code:
 if ([textField isEqualToString:@"fourthdimension"]) {

Almost anything else works, like:

Code:
 if ([textField intValue] == 2377854) {
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
This is where you learn to use the debugger. That would allow you to follow the path and investigate characteristics of the textField.

You could also use NSLog within each path as an investigative tool.
 
OP
M
Joined
Sep 14, 2008
Messages
18
Reaction score
0
Points
1
Debugging and viewing the log says "unrecognized selector sent to instance"

2008-12-23 22:14:50.758 GoodApp[22411:813] *** -[NSTextField isEqualToString:]: unrecognized selector sent to instance 0x123940

What does this mean?
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
Debugging and viewing the log says "unrecognized selector sent to instance"

2008-12-23 22:14:50.758 GoodApp[22411:813] *** -[NSTextField isEqualToString:]: unrecognized selector sent to instance 0x123940

What does this mean?

It's telling you that your receiver object doesn't support the selector you are trying to use (you know, in the [receiver selector] type of statement), and it helpfully identifies the mismatched values. Look at the type of your textField variable, and remember from the documentation that isEqualToString is a selector of the NSString class. This should make it obvious what you are doing wrong. (Hint: the fix involves something you were doing right in the first iteration of your code.)
 
OP
M
Joined
Sep 14, 2008
Messages
18
Reaction score
0
Points
1
Wait, though. textField is an outlet connected to an NSTextField object. It responds to other selectors involving strings... If I insert this before the if:

Code:
 NSLog([textField stringValue])

And it faithfully spits out whatever I type in.

If it's able to tell me its string value, why isn't it able to judge equality to another string??
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
Wait, though. textField is an outlet connected to an NSTextField object. It responds to other selectors involving strings... If I insert this before the if:

Code:
 NSLog([textField stringValue])

And it faithfully spits out whatever I type in.

If it's able to tell me its string value, why isn't it able to judge equality to another string??

NSTextField knows how to convert its contents to an NSString if the stringValue selector is called, true. But that doesn't mean that an NSTextField can be treated as is if it is an NSString ... they are still separate types of objects, with separate functionality.

Selectors only function when sent to classes that have them defined (either in the class itself or in one of the classes it inherits from). The stringValue selector is defined as a member of the NSControl class, from which NSTextField is subclassed (check the "Inherits from" section of the NSTextField documentation). Conversely, isEqualToString is a selector defined as part of the NSString class, and does not inherit anything from either NSTextField or NSControl. Hence, you can no more say [NSTextField isEqualToString] than you can say [NSString stringValue]. The latter would of course be redundant and effectively a no-op, but nonetheless it doesn't work ... try it for yourself and see.

If you want to use NSString selectors on the string contents of an NSTextField, first you need to get the contents of the NSTextField into an NSString object. You should already know how to do that ... in fact you just finished talking about doing it successfully, and it doesn't even have to require any additional lines of code. Hint: you can nest messages.
 
Joined
Jun 19, 2008
Messages
3
Reaction score
0
Points
1
A bit late, but here's how to do it:

if ([[textField stringValue] isEqualToString:mad:"fourthdimention"]) {
// execute this
}
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top