PDA

View Full Version : Can you help me understand isEqualToString: ?




Glama
Feb 11, 2009, 07:35 AM
Hello, The book I'm studying on Objective-C says that isEqualToString: is declared like this:

- (BOOL) isEqualToString: (NSSTRING *) aString;

I know that the BOOL means it returns a BOOL value, but I dont know how to read the rest of the line. The * means that NSSTRING is a pointer, correct? what is aString?

The book has this example on how to use it:

if ([thing1 isEqualToString: thing2]) {
NSLog (@"They are the same");
}

Again I am confused as to how to read the bolded line and relate it to the declaration. It seems weird that a parameter (thing1) is before the function (isEqualToString:). Can anyone help me understand the bolded line?



rastersize
Feb 11, 2009, 08:41 AM
- (BOOL) isEqualToString: (NSString *) aString;
"isEqualToString:" is a method which takes a pointer ("aString") to a NSString object and compares it to the NSString object it was called on.

if ([thing1 isEqualToString: thing2]) {
Here "thing1" is a pointer to a NSString object and we use it's member method "isEqualToString:" to compare it with the NSString object "thing2".

So "thing1" isn't an argument but an object which has a member method (or function if that's easier) called "isEqualToString:"

Glama
Feb 11, 2009, 09:01 AM
Great explanation, Rastersize. Thanks very much for the help.

ALEXROD
Jan 29, 2011, 10:15 AM
I'm having the same problem but it does not work, can you tell me what's wrong. (note:test 3 is the output from the keyboard dictionary it's a frame reference of the keyboard view.)


NSString *test3=[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSString *test2 = @"NSRect: {{0, 217}, {320, 263}}";


NSLog(@"value is-%@",test3);
NSLog(@"string is-%@",test2);

NSlog output is…. you see they are both equal testing should evaluate to true

2011-01-29 09:48:30.314 APFA5[15478:207] value is-NSRect: {{0, 217}, {320, 263}}
2011-01-29 09:48:30.315 APFA5[15478:207] string is-NSRect: {{0, 217}, {320, 263}}

but if I evaluate with...

if ([test2 isEqualToString:test3]){

NSLog(@"TRUE");
}
else
{
NSLog(@"FALSE");
}

NSLog output becomes…..

2011-01-29 09:58:25.300 APFA5[15701:207] value is-NSRect: {{0, 217}, {320, 263}}
2011-01-29 09:58:25.301 APFA5[15701:207] string is-NSRect: {{0, 217}, {320, 263}}
2011-01-29 09:58:25.301 APFA5[15701:207] FALSE

it evaluates as FALSE WHY, WHY, WHY, WHY!!!!!!!!! HOW DO I COMPARE THESE NSString values?

Sayer
Jan 29, 2011, 10:27 AM
I'm having the same problem but it does not work, can you tell me what's wrong. (note:test 3 is the output from the keyboard dictionary it's a frame reference of the keyboard view.)


NSString *test3=[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
NSString *test2 = @"NSRect: {{0, 217}, {320, 263}}";


NSLog(@"value is-%@",test3);
NSLog(@"string is-%@",test2);

NSlog output is…. you see they are both equal testing should evaluate to true

2011-01-29 09:48:30.314 APFA5[15478:207] value is-NSRect: {{0, 217}, {320, 263}}
2011-01-29 09:48:30.315 APFA5[15478:207] string is-NSRect: {{0, 217}, {320, 263}}

but if I evaluate with...

if ([test2 isEqualToString:test3]){

NSLog(@"TRUE");
}
else
{
NSLog(@"FALSE");
}

NSLog output becomes…..

2011-01-29 09:58:25.300 APFA5[15701:207] value is-NSRect: {{0, 217}, {320, 263}}
2011-01-29 09:58:25.301 APFA5[15701:207] string is-NSRect: {{0, 217}, {320, 263}}
2011-01-29 09:58:25.301 APFA5[15701:207] FALSE

it evaluates as FALSE WHY, WHY, WHY, WHY!!!!!!!!! HOW DO I COMPARE THESE NSString values?

NSRect is not a string, so you cant make a literal comparison. You may try isEqualTo instead, or comparing the actual values of the NSRect with each other.

ALEXROD
Jan 29, 2011, 10:38 AM
Thanks Sayer I appreciate your response. You gave me a good idea to compare frames instead of strings.

I tried isEqualToString but it always evaluates to FALSE even both values are equal. (Probably comparing memory pointers and not values)

But What I cant understand is that both values are output by NSLog as strings to the console, so how does NSLog do that. That would equalize both values so they can be compared...

Anyway thanks, again...

chown33
Jan 29, 2011, 11:32 AM
But What I cant understand is that both values are output by NSLog as strings to the console, so how does NSLog do that.

First, look at your formatting string:
@"value is-%@"

What are the print specifiers(the segments that begin with %)?

Next, look at the reference doc for the %@ print specifier:
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/Strings/Articles/formatSpecifiers.html%23//apple_ref/doc/uid/TP40004265-SW1

What does it say about %@?

ALEXROD
Jan 29, 2011, 12:31 PM
Ah, I see, It says I was trying to stuff an Elephant into a Pickle jar. I printed out the page, thanks for the reference...