通过字符串设置Objective-C属性 [英] Set Objective-C property by string

查看:110
本文介绍了通过字符串设置Objective-C属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个简单的方法来设置(或检索)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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆