如何将键值对附加到 iPhone 上的 UIView?

发布于 2024-07-11 13:11:29 字数 406 浏览 7 评论 0原文

当我开始 iPhone 开发时,我在某处读到可以将键值对附加到 UIView。 我知道所有 UIView 都可以用作字典来存储您可能想要附加到它们的任何数据,以防止不必要的子类化。 然而,我到处寻找参考,并尝试自己实现该行为,但徒劳无功。

我已经尝试过诸如以下的事情:

UIView *myView = [[UIView alloc] init];
[myView setValue:@"hello" forKey:@"world"];

但这似乎不起作用。 我认为上面的代码所做的是尝试将值@“hello”分配给属性@“world” - 这不是我想要的。

我想要实现的目标可能吗?

任何帮助将不胜感激。

谢谢!

缺口。

When I started iPhone development, I read somewhere that it's possible to attach a key value pair to a UIView. I understood that all UIViews could be used as dictionaries to store any data you may want to attatch to them to prevent unnecessary subclassing. However, I've searched everywhere to find the reference and tried to implement the behavior myself in vain.

I've tried things such as:

UIView *myView = [[UIView alloc] init];
[myView setValue:@"hello" forKey:@"world"];

but this doesn't seem to work. I think what the above code does is tried to assign the value @"hello" to the property @"world" - which isn't what I want.

Is what I'm trying to achieve possible?

Any help here would be much appreciated.

Thanks!

Nick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

残月升风 2024-07-18 13:11:29

UIView 与通用键上的键值不兼容。 如果是,那么您的代码示例确实会为键@“world”设置值@“hello”。 但是,是键值兼容的,因此以下内容可以工作:

UIView *myView = [[UIView alloc] init];
[myView.layer setValue: @"hello" forKey: @"world"];

UIViews are not key-value compliant on generic keys. If they were, then your code sample would indeed set the value @"hello" for the key @"world". However, layers are key-value compliant, so the following would work:

UIView *myView = [[UIView alloc] init];
[myView.layer setValue: @"hello" forKey: @"world"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文