PDA

View Full Version : NSString issue.


RedHatCore
2008.02.09, 10:21 AM
First of all, I have no experience programming in OS X, and very little experience programming in windows. I have read a book on C, but it was when I was very young. I'm having an issue with my first program. I'm attempting to write a program that will simply take a string and see if it is the "serial," and if it is, change a label to the serial. I've be back and forth trying to get it simply to compile, and I finally did, but it doesn't function like I would have expected. Everything expect this part of the program functions as normal:


#import "serial.h"

@implementation serial
- (void)clickButton:(id)sender;
{
NSString *text = [textField stringValue];

if (text == @"serialnum1234")
{
[label setStringValue:text];

}
}
@end

So the issue is, when I type my serial into the text field and press the OK butten, nothing happens.

The way I know that everything else in the program functions normally is due to the fact that:

NSString *text = [textField stringValue];
[label setStringValue:text];

That code does change the label to what I type in the text field when OK is clicked.

Thanks for any help.

Malarkey
2008.02.09, 11:50 AM
Your if-then comparision is comparing two pointer addresses to see if they're equal. Use if ( [text isEqualToString: @"serialnum1234"] ) instead.

RedHatCore
2008.02.10, 04:14 AM
Thanks. Do you think you could explain to me to as to why this works?

OneSadCookie
2008.02.10, 05:05 AM
because it does a character-by-character comparison of the two strings, rather than just checking if the left and right are exactly the same object, as you were doing.