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的更多相关文章

  1. Encapsulating Data 数据封装

    Objective-C中类的封装本质上其他OO语言没什么区别,不过在概念和书写表达上差异还是比较大的, Property属性 这里的Property并不是简单的类成员变量,而是OC中特有的可以为编译器 ...

  2. Programming with Objective-C ----------Encapsulating Data

    Most Properties Are Backed by Instance Variables By default, a readwrite property will be backed by ...

  3. 《Programming with Objective-C》第四章 Encapsulating Data

    Designated Initializer 不稳定的传送门 合成属性 Properties don’t always have to be backed by their own instance ...

  4. Programming With Objective-C---- Encapsulating Data ---- Objective-C 学习(三) 封装数据

      Programming with Objective-C Encapsulating Data In addition to the messaging behavior covered in t ...

  5. 为什么要在游戏开发中使用ECS模式

    http://www.richardlord.net/blog/why-use-an-entity-framework Why use an entity system framework for g ...

  6. 《Programming with Objective-C》

    苹果官方文档:不稳定的传送门 读书笔记共有以下几篇,其他的知识点不重要或者已经熟悉不需记录 <Programming with Objective-C>第三章 Working with O ...

  7. [转载]NoSQL by Martin Flower

    ============================================================== URL1 nosql ========================== ...

  8. 采用现代Objective-C

    多年来,Objective-C语言已经有了革命性的发展.虽然核心理念和实践保持不变,但语言中的部分内容经历了重大的变化和改进.现代化的Objective-C在类型安全.内存管理.性能.和其他方面都得到 ...

  9. iOS Developer Libray (中文版)-- About Objective-C

    该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...

随机推荐

  1. NPOI导出Excel表功能实现(多个工作簿)(备用)

    Excel生成操作类: 代码 using System; using System.Collections.Generic; using System.Text; using System.IO; u ...

  2. 精选37条强大的常用linux shell命令组合

    任务                             命令组合 1 删除0字节文件 find . -type f -size 0 -exec rm -rf {} \;find . type f ...

  3. watch 命令实时命令执行监控

    watch 命令   watch -d -n 1 'df; ls -FlAt /path' 在使用这条命令时你需要替换其中的 /path 部分,watch 是实时监控工具,-d 参数会高亮 显示变化的 ...

  4. Mac 上安装MySQL

    http://blog.neten.de/posts/2014/01/27/install-mysql-using-homebrew/ http://www.wkii.org/mac-os-x-ins ...

  5. JFinal 部署在 Tomcat 下推荐方法(转)

    首先明确一下 JFinal 项目是标准的 java web 项目,其部署方式与普通 java web 项目没有任何差别.Java Web 项目在 Tomcat 下部署有一些不必要的坑需要避免,所以撰写 ...

  6. linux上改变mysql数据文件的位置

    用软连接改变了/var/lib/mysql的位置,并设置好mysql.mysql的权限,但是发现还是不能启动. 发现/var/log/mysqld.log 150308 16:16:02 [Warni ...

  7. VPS技术介绍以及分析

    VPS的全称为Virtual Private Server,叫做虚拟专用服务器(Godaddy称之为Virtual Dedicated Server,VDS).就是利用各种虚拟化手段把单台物理服务器虚 ...

  8. UVa 11384 Help is needed for Dexter 正整数序列

    给定一个正整数 n ,你的任务使用最少的操作次数把序列 1, 2, 3, -- , n 中的所有数都变成 0 .每次操作可以从序列中选择一个或者多个数,同时减去一个相同的正整数.比如,1, 2, 3 ...

  9. table 锁定表头,出滚动对齐

    前一段时间来了一个汇总的需求,想锁定表头,这个问题在网上找了老半天,实现起来都比较麻烦,经过这几天的摸索终于找到一个简洁的处理方法 下面介绍一下如何处理的: 1.thead 和tbody 放两个tab ...

  10. php查询汉字的拼音首字母的函数

    function getfirst($str, $charset='utf8'){         $dict=array(         'a'=>0xB0C4,         'b'=& ...