This question already has an answer here:
this is bizarre. The following if statement is failing. What could be wrong?
NSDate *date = [NSDate date];
if ([date isMemberOfClass: [NSDate class]]) {
// Not executed.
}
This question already has an answer here:
this is bizarre. The following if statement is failing. What could be wrong?
NSDate *date = [NSDate date];
if ([date isMemberOfClass: [NSDate class]]) {
// Not executed.
}
It happens that classes like
NSArray
,NSDictionary
,NSString
andNSData
are class clusters. This concept is explained better in the documentation, and it means that you will not get a direct instance for that class.Due to the variety of "data" to be handled, the class has internal specialised subclasses; and when you create an instance it will be determined which of these internal subclasses is the best option, and your object will then be an instance of that subclass (not of
NSData
itself).In this case, if you need to check that, use
isKindOfClass:
which will be true for subclasses as well.Edit: Just as an additional example, calling
NSStringFromClass([obj class])
in these objects:Results in:
That means when you try to do that underneath it's different class (concrete implementation of NSDate) :)
More on class clusters here