Results 1 to 14 of 14
  1. #1


    Member Since
    Sep 14, 2008
    Posts
    18
    Cocoa programming issue
    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.

  2. #2

    rman's Avatar
    Member Since
    Dec 24, 2002
    Location
    Los Angeles, California
    Posts
    12,584
    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
    Cool
    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!

  3. #3

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    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:
    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.

  4. #4


    Member Since
    Mar 15, 2007
    Posts
    161
    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. )

  5. #5


    Member Since
    Sep 14, 2008
    Posts
    18
    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???

  6. #6

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    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?
    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.

  7. #7


    Member Since
    Sep 14, 2008
    Posts
    18
    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) {

  8. #8

    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    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.
    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.

  9. #9


    Member Since
    Sep 14, 2008
    Posts
    18
    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?

  10. #10


    Member Since
    Mar 15, 2007
    Posts
    161
    Specs:
    17" MacBook Pro, 2.33GHz C2D, 2GB RAM
    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.)

  11. #11


    Member Since
    Sep 14, 2008
    Posts
    18
    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??

  12. #12


    Member Since
    Mar 15, 2007
    Posts
    161
    Specs:
    17" MacBook Pro, 2.33GHz C2D, 2GB RAM
    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.

  13. #13


    Member Since
    Sep 14, 2008
    Posts
    18
    I see what you mean! That makes sense! Thanks!

  14. #14


    Member Since
    Jun 19, 2008
    Posts
    3
    A bit late, but here's how to do it:

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Cocoa Programming book by Aaron Hillegas question
    By Shogun in forum OS X - Development and Darwin
    Replies: 9
    Last Post: 04-16-2009, 03:31 PM
  2. Cocoa Programming for Mac OS X
    By NSNick in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 06-21-2008, 08:34 PM
  3. Cocoa question
    By Osiris22x in forum OS X - Development and Darwin
    Replies: 1
    Last Post: 04-09-2007, 04:37 AM
  4. C++, Carbon and cocoa
    By sLiCk_InCoNtRoL in forum Schweb's Lounge
    Replies: 4
    Last Post: 01-03-2005, 10:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •