gpt4 book ai didi

iphone - 在 Objective-C 中返回一个对象的所有属性

转载 作者:太空狗 更新时间:2023-10-30 03:25:40 25 4
gpt4 key购买 nike

是否可以返回 Objective-C 中对象实现的所有属性的列表?我知道这些属性只是 getter 和 setter,所以我不确定它是如何工作的。如果不能做到这一点,那么是否可以返回对象将响应的所有选择器?我正在寻找与 Ruby 中的“方法”方法类似的东西。

最佳答案

// Get properties list    
objc_property_t* class_copyPropertyList(Class cls, unsigned int *outCount);
// Get methods list
Method* class_copyMethodList(Class cls, unsigned int *outCount);

下面的代码会将UIImage类中实现的所有方法输出到控制台:

unsigned int count;
Method* methods = class_copyMethodList([UIImage class], &count);
for (size_t i = 0; i < count; ++i)
NSLog([NSString stringWithCString:sel_getName(method_getName(methods[i]))]);
free(methods);

关于iphone - 在 Objective-C 中返回一个对象的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2410081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com