Encapsulating Data
【Encapsulating Data】
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 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.
1 @property (readonly, getter=isFinished) BOOL finished;
The default behavior for a writeable property is to use an instance variable called _propertyName. You Can Customize Synthesized Instance Variable Names:
1 @implementation YourClass
2 @synthesize propertyName = instanceVariableName;
3 @end
You should always access the instance variables directly from within an initialization method because at the time a property is set, the rest of the object may not yet be completely initialized.
【Categories】
It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class.
If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.
【Represent Other Values Using Instances of the NSValue Class】
The NSNumber class is itself a subclass of the basic NSValue class, which provides an object wrapper around a single value or data item. In addition to the basic C scalar types, NSValue can also be used to represent pointers and structures.
The NSValue class offers various factory methods to create a value with a given standard structure, which makes it easy to create an instance to represent, for example, an NSRange, like the example from earlier in the chapter:
NSString *mainString = @"This is a long string";
NSRange substringRange = [mainString rangeOfString:@"long"];
NSValue *rangeValue = [NSValue valueWithRange:substringRange];
It’s also possible to create NSValue objects to represent custom structures. If you have a particular need to use a C structure (rather than an Objective-C object) to store information, like this:
typedef struct {
int i;
float f;
} MyIntegerFloatStruct;
you can create an NSValue instance by providing a pointer to the structure as well as an encoded Objective-C type. The @encode() compiler directive is used to create the correct Objective-C type, like this:
struct MyIntegerFloatStruct aStruct;
aStruct.i = ;
aStruct.f = 3.14; NSValue *structValue = [NSValue value:&aStruct
withObjCType:@encode(MyIntegerFloatStruct)];
Encapsulating Data的更多相关文章
- Encapsulating Data 数据封装
Objective-C中类的封装本质上其他OO语言没什么区别,不过在概念和书写表达上差异还是比较大的, Property属性 这里的Property并不是简单的类成员变量,而是OC中特有的可以为编译器 ...
- Programming with Objective-C ----------Encapsulating Data
Most Properties Are Backed by Instance Variables By default, a readwrite property will be backed by ...
- 《Programming with Objective-C》第四章 Encapsulating Data
Designated Initializer 不稳定的传送门 合成属性 Properties don’t always have to be backed by their own instance ...
- Programming With Objective-C---- Encapsulating Data ---- Objective-C 学习(三) 封装数据
Programming with Objective-C Encapsulating Data In addition to the messaging behavior covered in t ...
- 为什么要在游戏开发中使用ECS模式
http://www.richardlord.net/blog/why-use-an-entity-framework Why use an entity system framework for g ...
- 《Programming with Objective-C》
苹果官方文档:不稳定的传送门 读书笔记共有以下几篇,其他的知识点不重要或者已经熟悉不需记录 <Programming with Objective-C>第三章 Working with O ...
- [转载]NoSQL by Martin Flower
============================================================== URL1 nosql ========================== ...
- 采用现代Objective-C
多年来,Objective-C语言已经有了革命性的发展.虽然核心理念和实践保持不变,但语言中的部分内容经历了重大的变化和改进.现代化的Objective-C在类型安全.内存管理.性能.和其他方面都得到 ...
- iOS Developer Libray (中文版)-- About Objective-C
该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...
随机推荐
- linux 多处理器概念
Linux 提出了 Multi-Processing 的概念,它的调度器可以将操作系统的线程均分到各个核(或硬件线程)上去执行,以此达到并行计算的目的,从而也可以极大地提高系统的性能. 实现计数器 1 ...
- 基于Html5的爱情主题网站–表白神器(第二版)
第二版在第一版的基础上增加了一个动态3D的白云效果背景,鼠标悬浮在页面上云朵会向屏幕Z轴方向运动,在第一人称视角看来向着云朵方向前进的,由此形成一个伪3D效果.有点绕,直接看demo就能理解了.3D白 ...
- Spring无配置使用properties文件
利用@PropertySource注解加载 @Configuration @ComponentScan(basePackages="*") @PropertySource({&qu ...
- (六)6.14 Neurons Networks Restricted Boltzmann Machines
1.RBM简介 受限玻尔兹曼机(Restricted Boltzmann Machines,RBM)最早由hinton提出,是一种无监督学习方法,即对于给定数据,找到最大程度拟合这组数据的参数.RBM ...
- ORACLE RAC集群硬件资源管理与单节点的区别
硬件资源是由OS kernel管理的,应用软件是不能直接访问硬件的,必须通过OS kernel提供的API接口间接访问,OS kernel 除了要完成用户的请求,还通过进程调度等机制来控制多进程对资源 ...
- switchover步骤切换
主库 alter system switch logfile; alter system set log_archive_dest_state_2='defer'; select switchover ...
- 【转】Android Interface的使用
原文网址:http://www.3g-edu.org/Android_free/art115.htm Android应用程序采用Java语言编写,Java语法和C/C++有很大的相似性,但也有一些特别 ...
- Android 高仿UC浏览器监控剪切板弹出悬浮窗功能
UC浏览器应该是android手机里 最流行的浏览器之一了,他们有一个功能 相信大家都体验过,就是如果你复制了什么文字,(在其他app中 复制也有这个效果!,所以能猜到肯定是监控了剪切板),就会弹出一 ...
- MySQL与Oracle 差异比较之一数据类型
数据类型 编号 ORACLE MYSQL 注释 1 NUMBER int / DECIMAL DECIMAL就是NUMBER(10,2)这样的结构INT就是是NUMBER(10),表示整型:MYSQL ...
- Arduino 使用舵机库时 其它引脚输出怪异 解决方案
使用Servo.h时,不管你在初始化时用的是9还是10脚,都不要把这两个脚作为舵机以外的用途! 例: servo.attach(9); digitalWrite(10,1);//错,不能把第10脚用作 ...