测试 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
s 添加属性,则应查看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);
}