Why does valueForKey: on a UITextField throws an exception for UITextInputTraits properties?
运行此:
1 2 3 4 5 6 7 8 9 | @try { NSLog(@"1. autocapitalizationType = %d", [self.textField autocapitalizationType]); NSLog(@"2. autocapitalizationType = %@", [self.textField valueForKey:@"autocapitalizationType"]); } @catch (NSException *exception) { NSLog(@"3. %@", exception); } |
输出此:
1 2 | 1. autocapitalizationType = 0 3. [<UITextField 0x6c15df0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key autocapitalizationType. |
我期待:
1 2 | 1. autocapitalizationType = 0 2. autocapitalizationType = 0 |
此异常仅在
那么,为什么不能通过键值编码访问
如果您深入研究UIKit框架并打开
1 2 3 4 5 | @interface UITextField : UIControl <UITextInput, NSCoding> { @private UITextInputTraits *_traits; UITextInputTraits *_nonAtomTraits; |
您还将发现UITextField头文件中将
您和我看不到
这是我的变通办法:对于实现
以下是实施细节:1、2和3。