2

this is bizarre. The following if statement is failing. What could be wrong?

 NSDate *date = [NSDate date];

 if ([date isMemberOfClass: [NSDate class]]) {
    // Not executed.
 }
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
Doe X
  • 29
  • 3
  • Have you tried `isKindOfClass:` in place of `isMemberOfClass:`? You shouldn't need to use the more generic version of the two, but it might provide the result you're looking for. – BlueBear Sep 08 '15 at 19:37
  • have you looked into this :http://stackoverflow.com/questions/3653929/iphone-sdk-difference-between-iskindofclass-and-ismemberofclass ? – Teja Nandamuri Sep 08 '15 at 19:39
  • 3
    @Mr.T this is not a duplicate of that thread. OP is asking a question regarding an issue they have run into while using those methods, not what the difference between the two is. – BlueBear Sep 08 '15 at 19:44
  • @Jonathan I see your point, but you see OP's problem too, right? I just tried `isKindOfClass` and `isMemberOfClass` on `UIView` and both hold up. – Shamas S Sep 08 '15 at 19:47
  • It may not be a perfect duplicate as the question is different, but the link I provided will answer to this question in all aspects,which makes me think this one as duplicate. – Teja Nandamuri Sep 08 '15 at 19:50
  • moreover I alone cant close this question as duplicate. I just voted it as duplicate. – Teja Nandamuri Sep 08 '15 at 19:53

2 Answers2

4

It happens that classes like NSArray, NSDictionary, NSString and NSData 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.

NSDate *date = [NSDate date];

if ([date isKindOfClass: [NSDate class]]) {
    /* ... */
}

Edit: Just as an additional example, calling NSStringFromClass([obj class]) in these objects:

NSData   * data     = [NSData data];
NSData   * str_data = [@"string" dataUsingEncoding:NSUTF8StringEncoding];
NSNumber * n_bool   = [NSNumber numberWithBool:YES];
NSNumber * n_int    = [NSNumber numberWithInt:42];
NSArray  * array    = [NSArray array];

Results in:

_NSZeroData
NSConcreteMutableData
__NSCFBoolean
__NSCFNumber
__NSArrayI
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • You beat me to it. (voted). I often have trouble remembering which is which, isMemberOfClass and isKindOfClass, and have to check the docs if I haven't used them in a while. – Duncan C Sep 08 '15 at 20:37
  • @DuncanC Yes, and I personally don't use them much too. But I think "member" already brings a more restrict sense than "kind". I think you can remember that if `A` is almost like `B`, but not exactly equal, we can say that `A` is _kind of_ `B`.... So the _kind_ version is more broader. I can't think of a better mnemonic than that now. Maybe others will. The more strange or unreal it is, the better we will remember it :-D – sidyll Sep 08 '15 at 22:23
  • When I think about it I use the term "member" to be a synonym for instance. But then I spend 2 months without using it and forget my mnemonic. – Duncan C Sep 08 '15 at 23:16
  • 1
    thanks, this is pretty much what I suspected. – Doe X Sep 08 '15 at 23:26
0

NSDate is a class cluster.

That means when you try to do that underneath it's different class (concrete implementation of NSDate) :)

More on class clusters here

sloik
  • 694
  • 5
  • 12