在 Objective-C 中循环遍历对象属性并更改当前属性

发布于 2024-10-02 00:40:39 字数 937 浏览 3 评论 0原文

新手问题:可以在运行时用命令、属性或方法替换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱影子 2024-10-09 00:40:39

您想要的是键值编码。具体来说,您尝试编写的行是 [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]]].

有深☉意 2024-10-09 00:40:39

点符号是在编译时而不是运行时解析的,因此您不能将其用于此目的。为此,您必须使用设置器名称。即 setTheChangingProperty:performSelector: 以及适当的对象作为值传入。

请注意,在简单的情况下,这可能会很好地工作,但是,有人可能会去做类似的事情:

@property (nonatomic, retain, setter=fooBarBaz:) id blurgle;

在上述情况下,您不能简单地假设 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: and performSelector: 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:

@property (nonatomic, retain, setter=fooBarBaz:) id blurgle;

You in the above case, would not be simply able to assume setBlurgle: is the right setter.

权谋诡计 2024-10-09 00:40:39

您可以使用 setValue:forKey: 但请注意,属性键必须是 NSString,而不是 C 字符串,因此声明您的数组:

NSString* theChangingProperty[17] = { @"name", ....

然后您可以编写:

for (int t = 0; t < 17; t++) 
{
    [myObject setValue: valueOfAnotherArray[t] forKey: theChangingProperty[t]];
}

或者您可以使用 PerformSelector:withObject: 但您的数组需要包含选择器

SEL theChangingProperty[17] = { @selector(setName:), ....

for (int t = 0; t < 17; t++) 
{
    [myObject performSelector: theChangingProperty[t] withObject: valueOfAnotherArray[t]];
}

You can use setValue:forKey: but note that property keys need to be NSStrings, not C strings so declare your array:

NSString* theChangingProperty[17] = { @"name", ....

Then you would write:

for (int t = 0; t < 17; t++) 
{
    [myObject setValue: valueOfAnotherArray[t] forKey: theChangingProperty[t]];
}

Or you can use performSelector:withObject: but then your array needs to contain selectors

SEL theChangingProperty[17] = { @selector(setName:), ....

for (int t = 0; t < 17; t++) 
{
    [myObject performSelector: theChangingProperty[t] withObject: valueOfAnotherArray[t]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文