Access Objective-C property dynamically using the name of the property

前端 未结 2 1885
时光说笑
时光说笑 2020-12-03 17:18

I know the string name of a property of an object. How would I go about getting and setting that property using the string?

2条回答
  •  难免孤独
    2020-12-03 17:33

    @synthesize propertyName automates the generation of getter and setter methods.

    The compiler generates

    • - (id)propertyName;
    • - (void)setPropertyName;

    If you have a selector as NSString, you can use performSelector:NSSelectorFromString.
    e.g.:
    [object performSelector:NSSelectorFromString(@"propertyName") ...]

提交回复
热议问题