ios developer tiny share-20160830
来源:互联网 发布:构建中小企业网络v6.0 编辑:程序博客网 时间:2024/06/17 14:59
今天讲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:.
@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.
0 0
- ios developer tiny share-20160830
- ios developer tiny share-20160628
- ios developer tiny share-20160629
- ios developer tiny share-20160630
- ios developer tiny share-20160701
- ios developer tiny share-20160704
- ios developer tiny share-20160705
- ios developer tiny share-20160706
- ios developer tiny share-20160707
- ios developer tiny share-20160708
- ios developer tiny share-20160711
- ios developer tiny share-20160712
- ios developer tiny share-20160713
- ios developer tiny share-20160714
- ios developer tiny share-20160715
- ios developer tiny share-20160718
- ios developer tiny share-20160719
- ios developer tiny share-20160720
- Runloop--Configuring Run Loop Sources (配置 Runloop 的源)(含demo)
- 《全国计算机等级考试三级教程》章节学习记录——第三章.数据库结构设计
- SQL 学习
- startActivityForResult和setResult详解
- 最值得学习的10个C语言开源项目
- ios developer tiny share-20160830
- 使用Xcode工具symbolicatecrash将设备导出的 .crash 文件符号化 (Xcode7.3)
- spark中的Broadcast variables和Accumulator (广播变量和累加器)
- Socket网络编程
- Linux上搭建FTP服务器-从基础到进阶
- MySQL创建用户与授权方法
- Android的普通广播和有序广播
- APNS推送
- 线程消息处理机制