我正在尝试学习Objective-C中的反射.我已经找到了一些关于如何转储类的属性列表的重要信息,特别是在这里,但我想知道是否可以使用反射设置属性的值.
我有一个键(属性名称)和值(所有NSStrings)的字典.我想使用Reflection获取属性,然后将其值设置为我的字典中的值.这可能吗?还是我在做梦?
这与字典无关.我只是使用字典发送值.
像这个问题,但对于目标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);
}
Run Code Online (Sandbox Code Playgroud)