iPhone - setValue:forkey: 未记录

发布于 01-03 22:32 字数 1333 浏览 5 评论 0 原文

我的代码中有这些行:

[defaults setValue:someValue forKey:@"someValue"];
[animationGroup setValue:@"someValue" forKey:@"someValue"];  // CAAnimationGroup
NSString* animName = [theAnimation valueForKey:@"someValue"]; // CAAnimation

但是阅读Apple参考文档:

我不知道没有看到任何对这些方法的引用,甚至解析层次结构或一致的协议。他们从哪里来?允许使用它们吗?

I have these lines in my code :

[defaults setValue:someValue forKey:@"someValue"];
[animationGroup setValue:@"someValue" forKey:@"someValue"];  // CAAnimationGroup
NSString* animName = [theAnimation valueForKey:@"someValue"]; // CAAnimation

But reading the Apple Reference Documentation :

I don't see any reference to these methods, even parsing hierarchy or conformed protocols. From where do they come ? Is their use allowed ?

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

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

发布评论

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

评论(2

原谅我要高飞 2025-01-10 22:32:35

这些是键值编码方法;如果这些类符合这些键的键值编码,那么您可以毫无问题地使用它们。对于 Core Animation,CALayerCAAnimation 均符合 KVC,详细信息请参见 本编程指南。有关更多一般信息,请参阅 NSKeyValueCoding 非正式协议和 键值编码编程指南

These are Key-Value Coding methods; if those classes are Key-Value Coding compliant for those keys, then you can use them without issue. With respect to Core Animation, both CALayer and CAAnimation are KVC compliant, as detailed in this programming guide. For more general information, refer to the NSKeyValueCoding informal protocol and the Key-Value Coding Programming Guide.

情丝乱 2025-01-10 22:32:35

作为一个旁注,也让我感到困惑。上述链接之一提供:

CALayer和CAAnimation都是键值编码兼容的容器
类,允许您为任意键设置值。也就是说,同时
键“someKey”不是 CALayer 类的声明属性,
但是您仍然可以为键“someKey”设置一个值

这意味着您可以安全地发送类似 KVC 的 setValue:forKey: 和 valueForKey: 消息,而不必担心 @“someKey” 不是真正的 KVC 兼容的事实属性(因此是“任意键”)。如果您在其他对象上尝试此操作,可能会引发异常。我承认我不确定如何轻松找出其他类是否有这种行为,在许多其他地方免费提供这种行为肯定会很有用。

As a side note that confused me as well. One of the links above provides:

Both CALayer and CAAnimation are key-value coding compliant container
classes, allowing you to set values for arbitrary keys. That is, while
the key “someKey” is not a declared property of the CALayer class,
however you can still set a value for the key “someKey”

meaning that you can safely send KVC-like setValue:forKey: and valueForKey: messages and not have to worry about the fact that @"someKey" is not a real KVC-compliant property (hence "arbitrary keys"). If you try this on other objects, exceptions may be thrown. I admit I'm not sure how to find out easily if other classes have this behavior, it certainly would be useful to have this behavior for free in many other places.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文