我有一个函数,它接收一个对象、一个属性的名称及其值。我可以像这样简单地设置对象的属性吗:
-(void)dynamicSetterWithProperty:(NSString*)propertyThatIsKnownOnlyInRuntime
andValue:(NSString*)valueThatIsKnownOnlyInRuntime{
_myObject.propertyNameThatIsKnownOnlyInRuntime = valueNameThatIsKnownOnlyInRuntime;
}
还是我必须以这种丑陋的方式来做:
-(void)dynamicSetterWithProperty:(NSString*)propertyThatIsKnownOnlyInRuntime
andValue:(NSString*)valueThatIsKnownOnlyInRuntime{
if([propertyNameThatIsKnownOnlyInRuntime isEqualToString@"name"]){
_myObject.name = valueNameThatIsKnownOnlyInRuntime;
}
else if([propertyNameThatIsKnownOnlyInRuntime isEqualToString@"age"]){
_myObject.age = valueNameThatIsKnownOnlyInRuntime;
}
}