Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do we compare 2 class names of an object

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.

like image 899
Frank Avatar asked Dec 16 '09 21:12

Frank


People also ask

How do you compare classes in Java?

The compare() method in Java compares two class specific objects (x, y) given as parameters. It returns the value: 0: if (x==y) -1: if (x < y)

Can you compare two objects of the same class?

This can occur through simple assignment, as shown in the following example. Value equality means that two objects contain the same value or values. For primitive value types such as int or bool, tests for value equality are straightforward.

What is a comparison object?

Object comparison refers to the ability of an object to determine whether it is essentially the same as another object. You evaluate whether one object is equal to another by sending one of the objects an isEqual: message and passing in the other object.


2 Answers

Correct syntax is:

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

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

like image 79
mouviciel Avatar answered Sep 23 '22 17:09

mouviciel


This should do it:

NSString *bla = [[NSString alloc] init];
if ( [bla isMemberOfClass: [NSString class]] == YES )
     NSLog(@"Success");
like image 27
Chris Long Avatar answered Sep 24 '22 17:09

Chris Long