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 ...
随机推荐
- 使用Vmware虚拟机部署开发环境之Mac OS X系统安装
一.使用VMware虚拟机部署Swift开发环境所需工具: Vmware Workstation 10.0虚拟机软件 VM安装Mac解锁工具Unlock 苹果操作系统(Mac OS X Maveric ...
- hdu1521 排列组合(指数型母函数)
题意: 有n种物品,并且知道每种物品的数量ki.要求从中选出m件物品的排数. (全题文末) 知识点: 普通母函数 指数型母函数:(用来求解多重集的排列问题) n个元素,其中a1,a2, ...
- DayOfWeek
int a =(int)oneDate.DayOfWeek; 返回的直接就是 1,2,3,4,5,6, 星期日返回的是0
- Python 过算符和数据类型
一.算术运算符 二.逻辑运算符
- Mobile Safari调用本地App, 否则进入App Store下载
需求: 如何让用户通过手机浏览器(Mobile Safari),访问一个URL就能直接打开ios上的App应用,如果该应用没有安装,那么直接跳转到App Store的App下载页面. 准备工作 ios ...
- JAVA格物致知基础篇:你所不知道的返回码
上篇我们主要讲解利用Jersey组件如何来写一个能保证基本运行的Rest Service, 之所以说能够基本运行是因为接口暴露及其简易,一旦遇到其他的情况了,就无法正确的处理我们的请求.同时,这个接口 ...
- 日志文件清理工具V1.1
上次做完日志文件清理工具V1.0 的版本后,确实给自己的工作带来不少的方便.虽然只是一个小工具,代码也比较简单,但有用就是好东西.上次开发比较匆忙,有些细节没来得及完善,今天吃完晚饭,边看亚冠比赛边把 ...
- 年底发福利了——分享一下我的.NET软件开发资源
最近建了一个.NET软件开发资源的360网盘共享群,把收集的一些.NET软件开发资源分享给大家,也欢迎大家把好的东东分享一下. 资源主要有:开发工具.控件资源.书籍教程.网页设计.源码资源几大类,也希 ...
- C#稳固基础:传统遍历与迭代器
Hello,Coders.我们除了天天的码 if…else…之外,还会不断的码出foreach.我今天要说的是:传统遍历需实现的接口及我们还有一种更简洁优雅的方式实现多种迭代器. 传统遍历 传统的遍历 ...
- BZOJ 1005 [HNOI2008] 明明的烦恼(组合数学 Purfer Sequence)
题目大意 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为 1 到 N 的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 N( ...