通过字符串设置Objective-C属性 [英] Set Objective-C property by string
问题描述
我想知道是否有一个简单的方法来设置(或检索)Objective-C属性通过NSString ...可以使用 NSSelectorFromString
,但是我想要一个更容易(和更少的hacky方式)做它...像
I'm wondering if there's an easy way to set (or retrieve) an Objective-C property by a NSString... It is possible to use NSSelectorFromString
, but I'd like an easier (and less hacky way) to do it... Something like
[object setProperty:@"propertyValue" named:@"propertyName"];
相当于:
object.propertyName = @"propertyValue";
我真的不想混淆 @propertyName 命令的
并将其转换为
@ setPropertyName:
I don't really want to mess around with the @"propertyName"
and turn it into a @"setPropertyName:"
for the NSSelectorFromString
command... I may have no other choice, but if anyone knows a better way to do this, your help would be greatly appreciated!
推荐答案
如果有人知道更好的方法,使用键值编码。您的示例为 [object setValue:@propertyValueforKey:@propertyName]
。
Use Key-Value Coding. Your example would be [object setValue:@"propertyValue" forKey:@"propertyName"]
.
这篇关于通过字符串设置Objective-C属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!