为什么valueForKey:在UITextField上为UITextInputTraits属性抛出异常? [英] Why does valueForKey: on a UITextField throws an exception for UITextInputTraits properties?
问题描述
执行此操作:
@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:]:此类不是键值编码兼容的键autocapitalizationType。
我期待:
1。 autocapitalizationType = 0
2. autocapitalizationType = 0
此异常仅在属性的 UITextInputTraits
协议。 UITextField
的属性可以通过 clearButtonMode
> valueForKey:。
那么为什么不能用键来访问 UITextInputTraits
属性如果你深入到UIKit框架并打开 UITextField.h
@interface UITextField:UIControl< UITextInput,NSCoding> {
@private
UITextInputTraits * _traits;
UITextInputTraits * _nonAtomTraits;
您还会发现 clearButtonMode
在UITextField头文件中声明为 @property
,但 autocapitalizationType
(其余的 UITextInputTraits
协议)不是。
你和我不能看到 UITextField.m
,所以我们可以得出真正的结论是,苹果实现了
UITextField
的 UITextInputTraits
方式不符合KVC标准。可能胶水代码在某处转换 [myTextField autocapitalizationType]
到适当的值,但任何幕后魔法发生停止短于 valueForKey: / code>。
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?
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:
.
这篇关于为什么valueForKey:在UITextField上为UITextInputTraits属性抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!