ios developer tiny share-20160830
2016-09-01 16:21
197 查看
今天讲Objective的Accessor方法,即相当于java的getter和setter方法。
Use Accessor Methods to Get or Set Property Values
You access or set an object’s properties via accessor methods:
NSString *firstName = [somePerson firstName];
[somePerson setFirstName:@"Johnny"];
By default, these accessor methods are synthesized automatically for you by the compiler, so you don’t need to do anything other than declare the property using @property in the class interface.
The synthesized methods follow specific naming conventions:
The method used to access the value (the getter method) has the same name as the property. The getter method for a property called firstName will also be called firstName.
The method used to set the value (the setter method) starts with the word “set” and then uses the capitalized property name. The setter method for a property called firstName will be called setFirstName:.
If you don’t want to allow a property to be changed via a setter method, you can add an attribute to a property declaration to specify that it should be readonly:
@property (readonly) NSString *fullName;
As well as showing other objects how they are supposed to interact with the property, attributes also tell the compiler how to synthesize the relevant accessor methods.
In this case, the compiler will synthesize a fullName getter method, but not a setFullName: method.
Note: The opposite of readonly is readwrite. There’s no need to specify the readwrite attribute explicitly, because it is the default.
If you want to use a different name for an accessor method, it’s possible to specify a custom name by adding attributes to the property. In the case of Boolean properties (properties that have a YES or NO value), it’s customary for the getter method to start
with the word “is.” The getter method for a property called finished, for example, should be called isFinished.
Again, it’s possible to add an attribute on the property:
@property (getter=isFinished) BOOL finished;
If you need to specify multiple attributes, simply include them as a comma-separated list, like this:
@property (readonly, getter=isFinished) BOOL finished;
In this case, the compiler will synthesize only an isFinished method, but not a setFinished: method.
Note: In general, property accessor methods should be Key-Value Coding (KVC) compliant, which means that they follow explicit naming conventions.
See Key-Value Coding Programming Guide for more information.
Use Accessor Methods to Get or Set Property Values
You access or set an object’s properties via accessor methods:
NSString *firstName = [somePerson firstName];
[somePerson setFirstName:@"Johnny"];
By default, these accessor methods are synthesized automatically for you by the compiler, so you don’t need to do anything other than declare the property using @property in the class interface.
The synthesized methods follow specific naming conventions:
The method used to access the value (the getter method) has the same name as the property. The getter method for a property called firstName will also be called firstName.
The method used to set the value (the setter method) starts with the word “set” and then uses the capitalized property name. The setter method for a property called firstName will be called setFirstName:.
If you don’t want to allow a property to be changed via a setter method, you can add an attribute to a property declaration to specify that it should be readonly:
@property (readonly) NSString *fullName;
As well as showing other objects how they are supposed to interact with the property, attributes also tell the compiler how to synthesize the relevant accessor methods.
In this case, the compiler will synthesize a fullName getter method, but not a setFullName: method.
Note: The opposite of readonly is readwrite. There’s no need to specify the readwrite attribute explicitly, because it is the default.
If you want to use a different name for an accessor method, it’s possible to specify a custom name by adding attributes to the property. In the case of Boolean properties (properties that have a YES or NO value), it’s customary for the getter method to start
with the word “is.” The getter method for a property called finished, for example, should be called isFinished.
Again, it’s possible to add an attribute on the property:
@property (getter=isFinished) BOOL finished;
If you need to specify multiple attributes, simply include them as a comma-separated list, like this:
@property (readonly, getter=isFinished) BOOL finished;
In this case, the compiler will synthesize only an isFinished method, but not a setFinished: method.
Note: In general, property accessor methods should be Key-Value Coding (KVC) compliant, which means that they follow explicit naming conventions.
See Key-Value Coding Programming Guide for more information.
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- C# 6.0的属性(Property)的语法与初始值详解
- JS中如何设置readOnly的值
- C#中Property和Attribute的区别实例详解
- JavaScript中检查对象property的存在性方法介绍
- JavaScript中对象property的删除方法介绍
- JavaScript中遍历对象的property的3种方法介绍
- javascript 对象属性property与元素属性attribute的浏览器支持
- jquery设置元素的readonly和disabled的写法
- javascript中input中readonly和disabled区别介绍
- c#.net中const和readonly的区别
- JavaScript Table排序 2.0 (更新)
- jquery批量设置属性readonly和disabled的方法
- .NET中TextBox控件设置ReadOnly=true后台取不到值三种解决方法