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开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...
随机推荐
- 推荐一款开源的原型设计软件--pencil
如果觉得内置的元素不够,可以直接用类似屏幕截图软件直接剪切粘贴,并且可以制作自己的元素集合.很好用 http://pencil.evolus.vn/ Easy GUI Prototyping Penc ...
- Velocity模板中的注释
Velocity ——VTL模板中的注释 注释允许在模板中包含描述文字,而这些文字不会被放置到模板引擎的输出中.注释是一种有效的提醒自己和向别人解释你的VTL语句要做什么事情的方法.你也可以把注释用来 ...
- org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.
2011-08-16 13:26:58,484 [http-8080-1] ERROR [core.web.ExceptionInterceptor] - org.codehaus.jackson.m ...
- C#配置系统未能初始化
如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素.",将appSettings放到conf ...
- Qt之等待提示框(QPropertyAnimation)
简述 之前分享过QLabel可以通过QMovie播放gif图片,可以实现等待提示框,今天主要使用动画QPropertyAnimation来进行实现! 数据加载的时候,往往都需要后台线程进行数据请求,而 ...
- OC获取文件(夹)的代码行数
/* 考察NSString NSArray NSFileManager */ #import <Foundation/Foundation.h> /* 计算单个文件的代码行数 path:文 ...
- In App Purchase翻译
一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程序将从App Store接收那些你想要提供的产品的信息,并将它们显示出来供用户购买.当用户需要购 ...
- Android下EditText的hint的一种显示效果------FloatLabelLayout
效果: 此为EditText的一种细节,平时可能用的不多,但是用户体验蛮好的,特别是当注册页面的项目很多的时候,加上这种效果,体验更好 仅以此记录,仅供学习参考. 参考地址:https://gist. ...
- RDoc
RDoc - Ruby Documentation System home github.com/rdoc/rdoc rdoc docs.seattlerb.org/rdoc bugs github. ...
- 多种css3时尚侧栏菜单展开显示效果Off-Canvas Menu Effects
今天我们想分享多种css3时尚侧栏菜单展开显示效果.侧边栏应用广泛,我们之前已经产生了一些效果灵感.风格演变,今天我们要展示一套新的灵感的现代效果.不同的布局和菜单的同步转换和页面可以让一切看起来更有 ...