2015-03-03 19 views
0

我在我的viewController中设置了3 UITextField如果不先调用setValueForKey,我会得到什么值?

@property(nonatomic,retain) UITextField *line1; 
@property(nonatomic,retain) UITextField *line2; 
@property(nonatomic,retain) UITextField *line3; 

我尝试获取价值的关键:

for (int i = 1; i <= 3; i++) 
{ 
    NSString *fieldName = [NSString stringWithFormat:@"line%d",i ]; 
    UITextField *theField = [self valueForKey:fieldName]; 
    NSLog(@"TEXTFIELD : %@",theField); 
} 

而且我得到了所有UITextFields。

2015-03-03 10:32:32.776 KVCTEST[26691:1586602] TEXTFIELD : <UITextField: 0x7f9a00e1c5c0; frame = (50 120; 220 30); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7f9a00e1ae40>> 
2015-03-03 10:32:32.777 KVCTEST[26691:1586602] TEXTFIELD : <UITextField: 0x7f9a00f1b920; frame = (50 160; 220 30); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7f9a00f1bb80>> 
2015-03-03 10:32:32.777 KVCTEST[26691:1586602] TEXTFIELD : <UITextField: 0x7f9a00f1d330; frame = (50 200; 220 30); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7f9a00f1d590>> 

但是,如果我将实例变量名更改为例如LINE1,LINE2,LINE3,异常会提高。

那么key-value-coding会默认与ivar的名字相关联吗?首先我不需要setValueForKey

+0

你的答案是下valueForKey'默认搜索模式:'在[文件](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/SearchImplementation.html #// apple_ref/DOC/UID/20000955) – MendyK 2015-03-03 03:18:46

回答

0

您的回答可以在Apple documentation regarding KVC找到。

键是标识对象的特定属性的字符串。 通常,一个键对应于接收方中的访问方法或实例变量的名称。密钥必须使用ASCII 编码,以小写字母开头,并且可能不包含 空格。

相关问题