iPhone:如何获取一个类的所有属性?

发布于 2024-12-26 05:14:46 字数 709 浏览 2 评论 0原文

我有这个类:

@interface Mission : BaseModel  
{
NSString  *Description;
NSDate  *EndDate;
NSMutableArray  *MissionSectionList;
NSString  *Name;
NSDate  *StartDate;
}
@property (nonatomic, retain) NSString  *Description;
@property (nonatomic, retain) NSDate  *EndDate;
@property (nonatomic, retain) NSMutableArray  *MissionSectionList;
@property (nonatomic, retain) NSString  *Name;
@property (nonatomic, retain) NSDate  *StartDate;
@end

是否可以找出这个类的所有属性? 我尝试过

Ivar class_getClassVariable(Class cls, const char* name)

但我不知道名称应该是什么:

name:要获取的类变量定义的名称。

事实上我想在不知道名字的情况下得到所有这些。

这可能吗?

I have this class:

@interface Mission : BaseModel  
{
NSString  *Description;
NSDate  *EndDate;
NSMutableArray  *MissionSectionList;
NSString  *Name;
NSDate  *StartDate;
}
@property (nonatomic, retain) NSString  *Description;
@property (nonatomic, retain) NSDate  *EndDate;
@property (nonatomic, retain) NSMutableArray  *MissionSectionList;
@property (nonatomic, retain) NSString  *Name;
@property (nonatomic, retain) NSDate  *StartDate;
@end

Is it possible to find out all the atrributes of this class?
I tried it with

Ivar class_getClassVariable(Class cls, const char* name)

But I don't know what the name should be:

name: The name of the class variable definition to obtain.

And actually I want to get all of them without knowing the name.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2025-01-02 05:14:46

您可以使用class_copyIvarList函数获取该类的所有实例变量的列表。然后你可以迭代它来获取每个 ivar 的属性。 (请注意,您负责释放用于 ivars 数组的内存)。示例代码可能如下所示:

unsigned int count;
Ivar *varList = class_copyIvarList([Mission class],&count);
for (int i = 0; i < count; ++i){
    Ivar var = varList[i];
    // Do when you need with ivar
}
free(varList);

You can use class_copyIvarList function to get the list of all instance variables of the class. Then you can iterate through it to get attributes of each ivar. (Note that you're responsible for freeing memory used for ivars array). Sample code may look like:

unsigned int count;
Ivar *varList = class_copyIvarList([Mission class],&count);
for (int i = 0; i < count; ++i){
    Ivar var = varList[i];
    // Do when you need with ivar
}
free(varList);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文