您的位置:首页 > 移动开发 > Objective-C

How do I list all fields of an object in Objective-C?

2015-01-30 14:39 555 查看
http://stackoverflow.com/questions/1213901/how-do-i-list-all-fields-of-an-object-in-objective-c

As mentioned, you can use the Objective-C runtime API to retrieve the instance variable names:

unsigned int varCount;

Ivar *vars = class_copyIvarList([MyClass class], &varCount);

for (int i = 0; i < varCount; i++) {
Ivar var = vars[i];

const char* name = ivar_getName(var);
const char* typeEncoding = ivar_getTypeEncoding(var);

// do what you wish with the name and type here
}

free(vars);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: