Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Glama

macrumors member
Original poster
Feb 5, 2009
31
0
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

macrumors member
Apr 9, 2008
38
0
Code:
- (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.

Code:
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

macrumors member
Original poster
Feb 5, 2009
31
0
Great explanation, Rastersize. Thanks very much for the help.
 

ALEXROD

macrumors newbie
Jan 29, 2011
3
0
How About This

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

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
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

macrumors newbie
Jan 29, 2011
3
0
Thanks Sayer

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

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green

ALEXROD

macrumors newbie
Jan 29, 2011
3
0
reply

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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.