为什么class_copyPropertyList()存在两个id变量但具有相同Class类型的差异

时间:2014-02-09 22:06:48

标签: ios iphone

id appdel = [[UIApplication sharedApplication] delegate];  // <--- i have set this delegate to an instance of MyAppDelegate
unsigned int property_count;  // <--- it will have 0 
objc_property_t * property_list = class_copyPropertyList(appdel, &property_count);
the property_list will be nil;

但如果我使用它:

id casualcore_app_delegate = objc_getClass("MyAppDelegate");

unsigned int outCount, i;  // <--- outCount will be the correct property count
objc_property_t *properties = class_copyPropertyList(casualcore_app_delegate, &outCount);

for (i = 0; i < outCount; i++)
{
  objc_property_t property = properties[i];
  fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
}

请告诉我,为什么相同的函数调用会返回不同的结果?

2 个答案:

答案 0 :(得分:1)

这两个论点不一样。

id appdel = [[UIApplication sharedApplication] delegate];

是MyAppDelegate类的实例

id casualcore_app_delegate = objc_getClass("MyAppDelegate");

本身。

现在class_copyPropertyList()的第一个参数是,因此它可以正常工作 在第二个例子中,但不在第一个例子中。如果你改变了

objc_property_t * property_list = class_copyPropertyList(appdel, &property_count);

objc_property_t * property_list = class_copyPropertyList([appdel class], &property_count);

然后您的第一个示例也可以使用,并给出相同的结果。

答案 1 :(得分:0)

将您的第一个示例更新为:

id appdel = [[[UIApplication sharedApplication] delegate] class];

然后它将与第二个例子相同。