NSDate isMemberOfClass: [NSDate class] returns false? [duplicate]

冷暖自知 提交于 2019-12-11 03:44:54

问题


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

 NSDate *date = [NSDate date];

 if ([date isMemberOfClass: [NSDate class]]) {
    // Not executed.
 }

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/32465816/nsdate-ismemberofclass-nsdate-class-returns-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!