今天讲Objective-C的点语法,它是accessor方法的简写形式。
Dot Syntax Is a Concise Alternative to Accessor Method Calls
As well as making explicit accessor method calls, Objective-C offers an alternative dot syntax to access an object’s properties.
Dot syntax allows you to access properties like this:
NSString *firstName = somePerson.firstName;
somePerson.firstName = @"Johnny";
Dot syntax is purely a convenient wrapper around accessor method calls. When you use dot syntax, the property is still accessed or changed using the getter and setter methods mentioned above:
- Getting a value using somePerson.firstName is the same as using [somePerson firstName]
- Setting a value using somePerson.firstName = @"Johnny" is the same as using [somePerson setFirstName:@"Johnny"]
This means that property access via dot syntax is also controlled by the property attributes. If a property is marked readonly, you’ll get a compiler error if you try to set it using dot syntax.