如何将变量名称转换为字符串?
例:
由此:
NSString *someVariable
int otherVariable
Run Code Online (Sandbox Code Playgroud)
我想获得一个带有变量实际名称的NSString,无论它是什么类型.
所以,对于上面的两个变量,我想得到他们的名字(someVariable,otherVariable).
我设法用这段代码解决了我的问题:
导入objc运行时
#import <objc/runtime.h>
你可以枚举属性:
- (NSArray *)allProperties
{
unsigned count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableArray *rv = [NSMutableArray array];
unsigned i;
for (i = 0; i < count; i++)
{
objc_property_t property = properties[i];
NSString *name = [NSString stringWithUTF8String:property_getName(property)];
[rv addObject:name];
}
free(properties);
return rv;
}
Run Code Online (Sandbox Code Playgroud)
希望它可以帮助某人.
| 归档时间: |
|
| 查看次数: |
2369 次 |
| 最近记录: |