要測試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