Why is capitalized property name not giving an error in Objective-C / UITouch?

喜夏-厌秋 提交于 2019-12-23 16:51:50

问题


resultLabel is a UILabel. So why does

 resultLabel.Text= @"";

not give an error? It should be resultLabel.text.

Thanks for any insights.


回答1:


The default setter function for a property foo is setFoo:, with the first letter capitalized. Therefore both lines

resultLabel.text = @"";
resultLabel.Text = @"";

generate the same code

[resultLabel setText:@""];

This works only with the setter function, not with the getter:

NSString *x = self.text; // --> x = [self text]
NSString *x = self.Text; // --> x = [self Text]

As a consequence, you cannot have two read-write properties that differ only in the case of the first letter, this will generate a compiler error:

@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) NSString *Text;

self.text = @"foo";
// error: synthesized properties 'text' and 'Text' both claim setter 'setText:'


来源:https://stackoverflow.com/questions/15410905/why-is-capitalized-property-name-not-giving-an-error-in-objective-c-uitouch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!