views:

205

answers:

2

Is there a way to get compare class name betweeen 2 objects?

Like:

NSString *bla = [[NSString alloc] init];
if([bla class] isEqual: NSString])
 NSLog(@"success");

unsure if my syntax is correct.

+4  A: 

This should do it:

NSString *bla = [[NSString alloc] init];
if ( [bla isMemberOfClass: [NSString class]] == YES )
     NSLog(@"Success");
Chris Long
Of course, `== YES` is optional. It probably looks better without it too!
Chris Long
In fact, comparing your BOOLs to YES is a bad idea. The BOOL type is not constrained to YES and NO, and some methods return a BOOL that is neither.
Chuck
What methods return a `BOOL` that is neither?
Wevah
@Wevah: I wish I could remember, but it's been too long. They might actually have been fixed by now for all I know. At any rate, it's safer and no more difficult to test `something != NO` (and, of course, this is the check that an if-statement naturally performs, so it can be omitted in that context).
Chuck
+2  A: 

Correct syntax is:

if ([bla class] == [NSString class])

You can also use -isMemberOfClass: or -isKindOfClass: messages from NSObject protocol.

mouviciel