New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Cocoa programming issue


Post Reply New Thread Subscribe

 
Thread Tools
Meds

 
Member Since: Sep 14, 2008
Posts: 18
Meds is on a distinguished road

Meds is offline
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.
QUOTE Thanks
rman

 
rman's Avatar
 
Member Since: Dec 24, 2002
Location: Los Angeles, California
Posts: 12,591
rman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud ofrman has much to be proud of
Mac Specs: 2 x 3.0GHz Quad-Core, 6GB OS X 10.6.8 | 15in MacBook Pro 2.2GHz OS X 10.6.8 | 64GB iPad 2 WiFi

rman is offline
note: moved to a better forum

Life isn't about waiting for the storm to pass, It's about learning to dance in the rain!
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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:

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
mystic_fm

 
Member Since: Mar 15, 2007
Posts: 161
mystic_fm will become famous soon enough
Mac Specs: 17" MacBook Pro, 2.33GHz C2D, 2GB RAM

mystic_fm is offline
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. )
QUOTE Thanks
Meds

 
Member Since: Sep 14, 2008
Posts: 18
Meds is on a distinguished road

Meds is offline
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???
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Try removing "== YES" although I'm not sure it should matter.

Also, is textField defined as an NSString, or somthing else?

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
Meds

 
Member Since: Sep 14, 2008
Posts: 18
Meds is on a distinguished road

Meds is offline
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) {
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks
Meds

 
Member Since: Sep 14, 2008
Posts: 18
Meds is on a distinguished road

Meds is offline
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?
QUOTE Thanks
mystic_fm

 
Member Since: Mar 15, 2007
Posts: 161
mystic_fm will become famous soon enough
Mac Specs: 17" MacBook Pro, 2.33GHz C2D, 2GB RAM

mystic_fm is offline
Quote:
Originally Posted by Meds View Post
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.)
QUOTE Thanks
Meds

 
Member Since: Sep 14, 2008
Posts: 18
Meds is on a distinguished road

Meds is offline
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??
QUOTE Thanks
mystic_fm

 
Member Since: Mar 15, 2007
Posts: 161
mystic_fm will become famous soon enough
Mac Specs: 17" MacBook Pro, 2.33GHz C2D, 2GB RAM

mystic_fm is offline
Quote:
Originally Posted by Meds View Post
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.
QUOTE Thanks
Meds

 
Member Since: Sep 14, 2008
Posts: 18
Meds is on a distinguished road

Meds is offline
I see what you mean! That makes sense! Thanks!
QUOTE Thanks
Alexandre 24

 
Member Since: Jun 19, 2008
Posts: 3
Alexandre 24 is on a distinguished road

Alexandre 24 is offline
A bit late, but here's how to do it:

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

Post Reply New Thread Subscribe


« Lost in the underworld clutches of Terminal, the wicked beast | Darwine or Crossover? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Cocoa Programming book by Aaron Hillegas question Shogun OS X - Development and Darwin 9 04-16-2009 02:31 PM
Cocoa Programming for Mac OS X NSNick OS X - Development and Darwin 0 06-21-2008 07:34 PM
another battery issue rothko Apple Notebooks 8 05-28-2008 09:36 PM
10.5.2 is out! pastorjay OS X - Operating System 105 02-14-2008 11:06 AM
10.4.9 Update Kash OS X - Operating System 104 03-22-2007 08:08 PM

All times are GMT -4. The time now is 06:42 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?