if(objc_getAssociatedObject(self,& Key))为nil对象返回true

时间:2013-11-15 13:33:07

标签: ios objective-c

如何测试objc_getAssociatedObjectnil?以下说元素不是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 !");

1 个答案:

答案 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()上的注释时会打印“元素:值”。你的代码有何不同?