Programming with Objective-C ----------Encapsulating Data
Most Properties Are Backed by Instance Variables
By default, a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler.
An instance variable is a variable that exists and holds its value for the life of the object. The memory used for instance variables is allocated when the object is first created (through alloc), and freed when the object is deallocated.
Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.
Although it’s best practice for an object to access its own properties using accessor methods or dot syntax, it’s possible to access the instance variable directly from any of the instance methods in a class implementation. The underscore prefix makes it clear that you’re accessing an instance variable rather than, for example, a local variable:
- (void)someMethod {
|
NSString *myString = @"An interesting string"; |
_someString = myString; |
} |
In this example, it’s clear that myString is a local variable and _someString is an instance variable.
In general, you should use accessor methods or dot syntax for property access even if you’re accessing an object’s properties from within its own implementation, in which case you should use self:
一般来说,你应该使用访问器方法或者点语法来存取属性,即使你在一个对象自己的声明部分访问某个属性也应该遵循这个原则,当然这时你应该用self:
- (void)someMethod {
|
NSString *myString = @"An interesting string"; |
self.someString = myString; |
// or |
[self setSomeString:myString]; |
} |
The exception to this rule is when writing initialization, deallocation or custom accessor methods, as described later in this section.
Programming with Objective-C ----------Encapsulating Data的更多相关文章
- Encapsulating Data
[Encapsulating Data] The synthesized methods follow specific naming conventions: The method used to ...
- Encapsulating Data 数据封装
Objective-C中类的封装本质上其他OO语言没什么区别,不过在概念和书写表达上差异还是比较大的, Property属性 这里的Property并不是简单的类成员变量,而是OC中特有的可以为编译器 ...
- 《Programming with Objective-C》第四章 Encapsulating Data
Designated Initializer 不稳定的传送门 合成属性 Properties don’t always have to be backed by their own instance ...
- <Spark><Programming><Loading and Saving Your Data>
Motivation Spark是基于Hadoop可用的生态系统构建的,因此Spark可以通过Hadoop MapReduce的InputFormat和OutputFormat接口存取数据. Spar ...
- Programming Assignment 5: Burrows–Wheeler Data Compression
编程作业五 作业链接:Burrows-Wheeler Data Compression & Checklist 我的代码:MoveToFront.java & CircularSuff ...
- Programming With Objective-C---- Encapsulating Data ---- Objective-C 学习(三) 封装数据
Programming with Objective-C Encapsulating Data In addition to the messaging behavior covered in t ...
- 《Programming with Objective-C》
苹果官方文档:不稳定的传送门 读书笔记共有以下几篇,其他的知识点不重要或者已经熟悉不需记录 <Programming with Objective-C>第三章 Working with O ...
- Programming With Objective-C---- Introduction ---- Objective-C 学习(一)
About Objective-C Objective-C is the primary programming language you use when writing software for ...
- Toward Scalable Systems for Big Data Analytics: A Technology Tutorial (I - III)
ABSTRACT Recent technological advancement have led to a deluge of data from distinctive domains (e.g ...
随机推荐
- FineReport中hadoop,hive数据库连接解决方案
1. 描述 Hadoop是个很流行的分布式计算解决方案,Hive是基于hadoop的数据分析工具.一般来说我们对Hive的操作都是通过cli来进行,也就是Linux的控制台,但是,这样做本质上是每个连 ...
- sqlServer数据库实现不同库之间表迁移
(1) (2) 点击开始便进行数据库到库的迁移.
- jQuery ScrollPagination修改之后
jQuery ScrollPagination修改之后代码 /* ** Anderson Ferminiano ** contato@andersonferminiano.com -- feel fr ...
- ASP.net 实现禁止用户重复登录
本文先为大家介绍如何利用缓存Cache方便地实现此功能. Cache与Session这二个状态对像的其中有一个不同之处,Cache是一个全局对象,作用的范围是整个应用程序,所有用户:而Session是 ...
- c语言函数
听着函数这两个字多多少少都会觉得有点复杂吧.因为一想到函数大多数人都会想到f(x),但其实c语言里面的函数不是那样的,老师教了一个很好的理解方法函数就是个机器人,而这个机器人是你自己做出来的.所有不用 ...
- NPOI导出Excel合并表头写入公式
protected void Btn1_Click(object sender, EventArgs e) { //建立空白工作簿 IWorkbook workbook = new HSSFWorkb ...
- js阿拉伯数字转中文大写
function DX(n) { if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法"; var unit = "千百 ...
- JS组件系列——Bootstrap右键菜单解决方案:ContextMenu
前言:有段时间没发表随笔了,过个年人都变得懒了.新年刚来上班,今天正好得空,将去年遗留的两个小组件总结记录下.有朋友跟我说:你的bootstrap组件要能够形成一个可以满足一般项目需求的系列组件,才有 ...
- RedHat/Centos修改root密码
Linux主机忘记密码,只要你能接触物理主机都可以修改root密码的! Redhat6.x 5.x / Centos6.x 5.x 01.开机-空格/enter 02.e-编辑模式 CentO ...
- 【原创】相对完美的垂直居中popup(modal/dialog),无需监听window.resize事件
<table class="popup" style="position: absolute; background-color: black; border-ra ...