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:You’ll also find that
clearButtonModeis declared as a@propertyin the UITextField header file, but thatautocapitalizationType(and the rest of theUITextInputTraitsprotocol) are not.You and I don’t get to see
UITextField.m, so all we can really conclude is that Apple implemented theUITextField‘sUITextInputTraitsprotocol 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 ofvalueForKey:.