如何测试objc_getAssociatedObject
是nil
?以下说元素不是nil
,但是之前从未设置过关联对象。
static char orderedElementKey = 11;
if (objc_getAssociatedObject(self, &orderedElementKey) != nil)
{
NSLog(@"element is not nil");
return objc_getAssociatedObject(self, &orderedElementKey);
}
NSLog(@"elelement was nil !");
答案 0 :(得分:1)
static char orderedElementKey = 11;
//objc_setAssociatedObject(self, &orderedElementKey, @"value", OBJC_ASSOCIATION_RETAIN);
if (objc_getAssociatedObject(self, &orderedElementKey) != nil)
{
NSLog(@"Element: %@", objc_getAssociatedObject(self, &orderedElementKey));
}
else
{
NSLog(@"element nil !");
}
打印“Element nil!”删除objc_setAssociatedObject()
上的注释时会打印“元素:值”。你的代码有何不同?