1

What is the syntax to say

if (object isObjectOfClass Class){ object.color = 1; }

I'm making a map application and i want to say "if this point on the map is a branch of THIS store- set the pin picture as this"

Jordan Brown
  • 638
  • 1
  • 5
  • 14

2 Answers2

6
if([myObj isKindOfClass:[MyClass class]]) {
   // ...
}
Seamus Campbell
  • 17,816
  • 3
  • 52
  • 60
  • 3
    Or `isMemberOfClass:` if you want to detect whether it's an instance of that precise class rather than a subclass — but generally you don't want to do that, and in the one case where it might make sense (differentiating mutable strings and arrays from their immutable cousins), it doesn't work right. – Chuck May 31 '11 at 23:07
  • Chuck beat me to it, but do note that [there is a difference between `isMemberOfClass` and `isKindOfClass`](http://stackoverflow.com/questions/2045561/objective-c-iskindofclass-missunderstanding). – Chris Frederick May 31 '11 at 23:07
3

What you're looking for is the NSObject isKindOfClass method.

if([object isKindOfClass:[ClassName class]) { object.color = 1};
Tim C
  • 2,920
  • 1
  • 15
  • 4