今天讲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"]
本文介绍了Objective-C中点语法的应用,这是一种访问对象属性的便捷方式。通过使用点语法,开发者可以更简洁地调用getter和setter方法来读取或设置属性值。需要注意的是,对于标记为只读的属性,尝试使用点语法进行修改会引发编译错误。
232

被折叠的 条评论
为什么被折叠?



