如何在 Objective-C 中运行时引用实例属性
Flash Actionscript 开发人员可以使用一个技巧来在运行时引用实例属性。我想知道 Objective-C 中是否存在类似的东西
在 actionscript 中我们可以做:
var thisObject;
for (var i=0; i<10; i++) {
thisObject = this["myInstanceProperty"+i];
thisObject.doSomething();
}
我认为 Objective-C 中会有类似的方法,但我找不到任何地方提到的任何内容。我正在寻找类似以下内容的内容:
for (int i=0; i<10; i++) {
NSString *buttonName = [NSString stringWithFormat:@"button_%i", i];
id *thisButton = [self instancePropertyWithStringName:buttonName];
thisButton.label = @"button %i";
}
你能明白我的意思吗?我有一个将视图链接到 IBOutlet 的 xib,我想在 for 循环中引用这些 IBOutlet,这样我就可以在运行时动态地向它们添加属性。
有什么想法吗?
There is a trick Flash Actionscript developers can do to refer to instance properties at runtime. I was wondering if anything similar existed in Objective-C
In actionscript we can do:
var thisObject;
for (var i=0; i<10; i++) {
thisObject = this["myInstanceProperty"+i];
thisObject.doSomething();
}
I thought there would be a method similar to this in Objective-C, but I can't find anything mentioned anywhere. I'm looking for something along the lines of:
for (int i=0; i<10; i++) {
NSString *buttonName = [NSString stringWithFormat:@"button_%i", i];
id *thisButton = [self instancePropertyWithStringName:buttonName];
thisButton.label = @"button %i";
}
Can you see what I'm getting at? I have a xib linking views to IBOutlets, and I'd like to refer to those IBOutlets from within a for loop, so I can add properties to them dynamically at runtime.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
self
对象符合NSKeyValueCoding
,则可以使用以下内容 - 默认情况下它对其实例变量和属性执行此操作。You can use the following if the
self
object conforms toNSKeyValueCoding
-- which it does by default for its instance variables and properties.听起来您正在寻找 Key -值编码指南。
It sounds like you're looking for a Key-Value Coding Guide.