在 Objective-C 中循环遍历对象属性并更改当前属性
新手问题:可以在运行时用命令、属性或方法替换 NSStrings 或 char 吗?
可以说我做了这样的事情:
//these are the names of my properties of an NSObject I created
char theChangingProperty[17][18] = {"name", "title", "number", "property4", "property5", "property6", "property7", "property8", "property9", "property10", "property11", "property12", "property13", "property14", "property15", "property16", "property17"};
(PS我知道有一种更好的实际 Objective-C 方法来获取对象属性,我什至找到了一篇带有此 在 Objective-C 中获取对象属性列表)
无论如何,如何迭代列表并更改属性名称?假设我在循环期间在运行时使用点表示法...如果我想将对象的 17 个(或 100 个!)不同属性一次全部设置为其他数组的值,如下所示:
for (int t=1; t<17; t++) {
myObject.theChangingProperty[t] = valueOfAnotherArray[t];
}
我知道目标 - c 将寻找一个名为“theChangingProperty”的实际属性。但我希望它是一个数组,它将吐出随着每次循环迭代而改变的实际属性(名称、标题、数字、属性4等)
感谢您花时间回答新手问题:)
Newbie question: Can one substitue commands, properties or methods for NSStrings or char at runtime?
Lets say I do something like this:
//these are the names of my properties of an NSObject I created
char theChangingProperty[17][18] = {"name", "title", "number", "property4", "property5", "property6", "property7", "property8", "property9", "property10", "property11", "property12", "property13", "property14", "property15", "property16", "property17"};
(PS I know there is a better actual objective-c way of getting an objects properties, I even found a post with this Get an object properties list in Objective-C)
anyway, how does one iterate through a list and have the property name change? Lets say I'm using dot notation at runtime during a loop... If I wanted to set 17 (or 100!) different properties of my object all to values of some other array all at once, like this:
for (int t=1; t<17; t++) {
myObject.theChangingProperty[t] = valueOfAnotherArray[t];
}
I know objective-c is going to look for an actual property called "theChangingProperty". But I want that to be an array that will spit out the actual property that would change with each loop iteration (name, title, number, property4, etc)
Thanks for your time in answering a newbie question :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要的是键值编码。具体来说,您尝试编写的行是
[myObject setValue:valueOfAnotherArray[t] forKey:[NSString stringWithUTF8String:theChangingProperty[t]]]
。What you want is called key-value coding. Specifically, the line you're trying to write is
[myObject setValue:valueOfAnotherArray[t] forKey:[NSString stringWithUTF8String:theChangingProperty[t]]]
.点符号是在编译时而不是运行时解析的,因此您不能将其用于此目的。为此,您必须使用设置器名称。即
setTheChangingProperty:
和performSelector:
以及适当的对象作为值传入。请注意,在简单的情况下,这可能会很好地工作,但是,有人可能会去做类似的事情:
在上述情况下,您不能简单地假设
setBlurgle:
是右二传手。Dot notation is resolved at compile time, not runtime, therefore you cannot use it for this purpose. You'll have to use the setter name for this. i.e.,
setTheChangingProperty:
andperformSelector:
with the appropriate object to pass in as the value.Note that in a simple case, this will probably work just fine, however, someone could have gone in and done something like:
You in the above case, would not be simply able to assume
setBlurgle:
is the right setter.您可以使用 setValue:forKey: 但请注意,属性键必须是 NSString,而不是 C 字符串,因此声明您的数组:
然后您可以编写:
或者您可以使用 PerformSelector:withObject: 但您的数组需要包含选择器
You can use setValue:forKey: but note that property keys need to be NSStrings, not C strings so declare your array:
Then you would write:
Or you can use performSelector:withObject: but then your array needs to contain selectors