【发布时间】:2014-08-28 00:15:56
【问题描述】:
我正在尝试学习 Objective-C 中的反射。我找到了一些关于如何转储类的属性列表的重要信息,尤其是here,但我想知道是否可以使用反射设置属性的值。
我有一个键(属性名)和值的字典(全部为NSStrings)。我想使用 Reflection 来获取属性,然后将其值设置为我的字典中的值。这可能吗?还是我在做梦?
这与字典无关。我只是使用字典来发送值。
类似于 this question,但针对 Objective C。
- (void)populateProperty:(NSString *)value
{
Class clazz = [self class];
u_int count;
objc_property_t* properties = class_copyPropertyList(clazz, &count);
for (int i = 0; i < count ; i++)
{
const char* propertyName = property_getName(properties[i]);
NSString *prop = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
// Here I have found my prop
// How do I populate it with value passed in?
}
free(properties);
}
【问题讨论】: