Michael Rogers
Michael Rogers

Reputation: 1349

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

resultLabel is a UILabel. So why does

 resultLabel.Text= @"";

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

Thanks for any insights.

Upvotes: 3

Views: 260

Answers (1)

Martin R
Martin R

Reputation: 539745

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:'

Upvotes: 7

Related Questions