为什么 UITextField 上的 valueForKey: 会引发 UITextInputTraits 属性异常?

发布于 2024-11-18 21:32:51 字数 823 浏览 3 评论 0原文

运行此:

@try
{
    NSLog(@"1. autocapitalizationType = %d", [self.textField autocapitalizationType]);
    NSLog(@"2. autocapitalizationType = %@", [self.textField valueForKey:@"autocapitalizationType"]);
}
@catch (NSException *exception)
{
    NSLog(@"3. %@", exception);
}

输出此:

1. autocapitalizationType = 0
3. [<UITextField 0x6c15df0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key autocapitalizationType.

我期望:

1. autocapitalizationType = 0
2. autocapitalizationType = 0

此异常仅发生在属于 UITextInputTraits 协议的属性上。 UITextField 的常规属性(例如具有 clearButtonMode)可以通过 valueForKey: 访问。

那么为什么不能使用键值编码访问 UITextInputTraits 属性呢?

Running this:

@try
{
    NSLog(@"1. autocapitalizationType = %d", [self.textField autocapitalizationType]);
    NSLog(@"2. autocapitalizationType = %@", [self.textField valueForKey:@"autocapitalizationType"]);
}
@catch (NSException *exception)
{
    NSLog(@"3. %@", exception);
}

Outputs this:

1. autocapitalizationType = 0
3. [<UITextField 0x6c15df0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key autocapitalizationType.

I was expecting:

1. autocapitalizationType = 0
2. autocapitalizationType = 0

This exception only happens with properties that are part of the UITextInputTraits protocol. Regular properties of a UITextField such has clearButtonMode can be accessed through valueForKey:.

So why can't you access UITextInputTraits properties with key-value coding?

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

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

发布评论

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

评论(2

帅气称霸 2024-11-25 21:32:51

如果您深入研究 UIKit 框架并打开 UITextField.h,您会发现:

@interface UITextField : UIControl <UITextInput, NSCoding> {
  @private

    UITextInputTraits  *_traits;
    UITextInputTraits  *_nonAtomTraits;

您还会发现 clearButtonMode 被声明为 @property< /code> 在 UITextField 头文件中,但 autocapitalizationType (以及 UITextInputTraits 协议的其余部分)不是。

你和我都看不到 UITextField.m,所以我们真正能得出的结论是 Apple 实现了 UITextFieldUITextInputTraits 协议在某种程度上不符合 KVC 标准。据推测,某处的粘合代码会将 [myTextField autocapitalizationType] 转换为适当的值,但无论发生什么幕后魔法,都会停止 valueForKey:

If you delve into the UIKit framework and open up UITextField.h, you'll find:

@interface UITextField : UIControl <UITextInput, NSCoding> {
  @private

    UITextInputTraits  *_traits;
    UITextInputTraits  *_nonAtomTraits;

You'll also find that clearButtonMode is declared as a @property in the UITextField header file, but that autocapitalizationType (and the rest of the UITextInputTraits protocol) are not.

You and I don't get to see UITextField.m, so all we can really conclude is that Apple implemented the UITextField's UITextInputTraits protocol in a way that's not KVC compliant. Presumably glue code somewhere converts [myTextField autocapitalizationType] into the appropriate value, but whatever behind-the-scenes magic is taking place stops short of valueForKey:.

用心笑 2024-11-25 21:32:51

这是我的解决方法:我为每个实现 textInputTraits 方法的类混合了 valueForKey: 。如果键是 UITextInputTraits 键,则在对象的 textInputTraits 而不是对象本身上调用 valueForKey:

以下是实现细节:123

Here is my workaround: I swizzled valueForKey: for every class implementing the textInputTraits method. If the key is a UITextInputTraits key, then call valueForKey: on the object's textInputTraits instead of the object itself.

Here are the implementation details: 1, 2 and 3.

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