要测试ID对象是NSNotification
使用:
[object isMemberOfClass:[NSNotification class]];`
为了测试是否是一个NSConcreteNotifications
使用
[object isMemberOfClass:NSClassFromString(@"NSConcreteNotifications")];
改变字符串到不同类的名称需要...
然后,您可以将两个检查合并为'NSNotification的子类(但不是NSConcreteNotification)。
或者:
if ([object isMemberOfClass:NSClassFromString(@"NSConcreteNotifications")])
{
// It's a NSConcreteNotifications...
}
else if ([object isKindOfClass:[NSNotification class]])
{
// It's an NSNotification (or subclass) but not an NSConcreteNotifications
}
或者
if ([object isKindOfClass:[NSNotification class]] && ![object isMemberOfClass:NSClassFromString(@"NSConcreteNotifications")])
{ /* ... */ }
如果你想将属性添加到NSNotification
是你应该看看Associative References。
的基本思路是:
static const char objectKey;
- (id)object
{
return objc_getAssociatedObject(self, &objectKey);
}
- (void)setObject:(id)object
{
objc_setAssociatedObject(self, &objectKey, object, OBJC_ASSOCIATION_RETAIN);
}
您测试的对象是NSNotification no的子类? – Geoffroy
您可以使用objective-C的isMemberOfClass方法。请参阅[isMemberOfClass] [1] [1]:http://stackoverflow.com/questions/2045561/objective-c-iskindofclass-missunderstanding – Naved
的对象可以是1,一种NSConcreteNotification或2。 NSNotification的子类。我需要分辨差异。 – Undistraction