Objective-C Properties

Apple introduced properties, a combination of new compiler directives
and a new attribute accessor syntax.

Apple 引入了属性这一概念。新的编译器命令和新的属性存取句法。

1.1 Shrinking the Interface 减少interface代码

 

@property float rainHandling; says that objects of the class AllWeatherRadial have an attribute, of type float, called rainHandling. It also says that you can set the property by calling –setRainHanding: and that you can access the attribute by calling -rainHandling.

@property float rainHandling;这个说明这个对象有一个类属性,是float 类型,名字为rainHandling 。同样,你能够通过调用setRainHanding 设置属性值。

1.2 Shrinking the Implementation

@synthesize rainHandling;

@synthesize snowHandling;

@synthesize is a compiler feature that says "create the accessors for this attribute."

@ synthesize是编译器特色,说创建这个属性的获取。 

 

@synthesize is not code generation. You won't ever see the code that implements –setRainHandling: and –rainHandling, but these methods will exist and will be callable.

@synthesize 不是代码生成器。但是这项方法存在,且能被调用。

If you provide the methods in your class for the property, the compiler doesn't create them. The compiler only creates methods that are missing.

如果你提供了这个属性的一个方法,那么compiler将不创建这些方法。compiler

只创建缺失的方法。

Most properties are backed by a variable, so when you synthesize the getter and setter, the compiler automatically creates an instance variable with the same name as the property.

大部分properties 返回一个variable ,所以当你同步getter 和setter方法的时候,compiler 自动创建了与这个属性名字相同的实例变量。

Notice that the header file has two variables called rainHandling and snowHandling. The setter and getter will use these variables. If you don't declare those variables, the compiler creates them.

注意头文件有两个变量,如果没写的话,compiler将自动添加这两个方法。

1.3 Dot notation 

The dot notation looks a lot like structure access in C and object access in Java—on purpose.

点运算符想存储在C和对象存取在java中一样

1.4 Objecting to Properties 对象 对于属性

Objects bring some added complications.

对象让事情麻烦了。

Recall that we retain and release objects as they flow through our accessors.

当他们要经过accessor时,我们retain 和release 对性。

For some object values, particularly string values, you want to always -copy them. Yet for other object values, like delegates (which we'll talk about in the next chapter), you don't want to retain them at all.

像string你想copy 它,而对于delegate ,你却不想retain 他们。

@property (copy) NSString *name;

@property (retain) Engine *engine;

 

 

You can use some other decorations, like nonatomic, which makes accessors a bit faster if they won't be used in a multithreaded environment.

如果 他们不用在多线程环境,nonatomic 使得accessor更快,

 

s. You can also use assign if you don't want the attribute object to be retained, to help avoid retain cycles.

如果你不想属性对象被retained ,那么我们将尽量避免retian 循环。

You can only specify retain and copy attributes for retainable pointers (i.e. Objective-C objects). All other types, such as C and nonretainable pointers, must use assign and manage memory manually.

 

If you provide either or both the setter and getter yourself, you cannot use atomic attribute; you must use nonatomic.

如果你自己写了setter 或getter方法中任意一个,那么你不能用atomic 属性,必须使用nontomic 属性。

 

1.5 Appellation spring  别名

the name of the property has been the same as the name of an instance variable that backs that property.

一般情况下,property的名字应该和返回的property的实例变量名字一样。 

Sometimes, though, you may want one name for the instance variable and another for the public attribute name.

但是有些时候你想让实例变量一个名字,而公共属性名字为另一个。

@interface Car : NSObject

{

  NSString *appellation;

  NSMutableArray *tires;

  Engine *engine;

}

@property (copy) NSString *name;

@property (retain) Engine *engine;

and then change the synthesize directive: @synthesize name = appellation;

 

In init, change

name = @"Car";

to

self.name = @"Car";

 

 

 

What's that self-dot-name business? It's a bit of disambiguation to let the compiler know that we want to vector through the accessors. If we just use a naked name, the compiler assumes that we're directly modifying an instance variable. To go through the accessors, we can write [self setName:@"Car"]. Remember that the dot is just shorthand for making this exact same call, so self.name = @"Car" is just another way of saying the same thing.

 

只是为了去除歧义。

 

1.6 Read-Only About It只读 

You might have an object with an attribute that is read-only.

有一些属性你允许它只能读。

You can code for these situations with more attributes on @property.

你可以在@property中加attribute

 

By default, properties are mutable: you can read and write them. Properties have a readwrite attribute you can use for specifying this.

默认情况下,properties 是可变的。你能读和写他们。

@property (readonly) float shoeSize;

@property (readonly) NSString *licenseNumber;

When the compiler sees that @property is readonly, it generates a getter but not a setter for that attribute.

当compiler 看到@property 只读时,应该只产生getter方法,而不产生setter 方法。

1.7 

What if you'd rather not have a variable, getter, and setter for your property?

如果你getter 和setter方法都不想要的话怎么办?

You can use the keyword @dynamic to tell the compiler not to generate any code or create a variable for the property.

你可以用关键则@dynamic 来告诉编译器不产生任何代码。

@property (readonly) float bodyMassIndex;

@dynamic bodyMassIndex;

- (float)bodyMassIndex

{

 ///compute and return bodyMassIndex

}

1.8 chang the method name

you might not like the names of the methods that are generated by default. They're in the form of blah and setBlah:.

有时候你想用自己的方法名字。

To overcome this, you can specify the names of the getter and setter that the compiler generates. Use the attributes getter= and setter= to define the preferred names of your methods.

为此,你应该在@property设置getter= and setter attribute . 

@property (getter=isHidden) BOOL hidden;

1.9 properties 并不是无所不能

- (void) setTire: (Tire *) tire atIndex: (int) index;

- (Tire *) tireAtIndex: (int) index;

these methods don't fit into the fairly narrow range of methods that properties cover. Properties will let you replace only –setBlah and –blah methods, but not methods that take extra arguments, like the tire's position on the car.

上面的方法并不适合properties .因为属性仅仅允许你替换setter和getter 方法不带参数的部分。

Objective-C Properties的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

  3. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  4. Objective C运行时(runtime)技术的几个要点总结

    前言:          Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表.属性列表.变量列表,修改方法.属性,增加方法,属性等等,本文对相 ...

  5. Objective C静态代码扫描和代码质量管理 OClint + SonarQube

    OClint是针对C, C++及Objective C代码的静态扫描分析工具,而SonarQube是一个开源的代码质量管理平台.本文将实现将OClint的扫描结果导入到SonarQube中,已实现对O ...

  6. Objective C Runtime 开发介绍

    简介 Objective c 语言尽可能的把决定从编译推迟到链接到运行时.只要可能,它就会动态的处理事情.这就意味着它不仅仅需要一个编译器,也需要一个运行时系统来执行变异好的代码.运行时系统就好像是O ...

  7. C++ vs Objective C

    oc Short list of some of the major differences: C++ allows multiple inheritance, Objective-C doesn't ...

  8. Objective C运行时(runtime)技术总结,好强大的runtime

    前言:          Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表.属性列表.变量列表,修改方法.属性,增加方法,属性等等,本文对相 ...

  9. Objective -C Categories

    Objective -C Categories  The dynamic runtime dispatch mechanism employed by Objective-C lets you add ...

  10. spring无法读取properties文件数据

    只讲述异常点,关于怎么配置文件,这里不做说明.   1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-cont ...

随机推荐

  1. 图像处理之基础---很好的一个快速比较两副图片是否相同的code 可用于公安鉴别

    转自Codeproject http://www.codeproject.com/dotnet/comparingimages.asp Public Enum CompareResult ciComp ...

  2. 2016/05/06 Sublime Text 3 常用插件以及安装方法(转)

    http://www.cnsecer.com/460.html 安装Sublime Text 3插件的方法: 朋友们,小站活着不容易,全靠广告费养着了,如果本文对你有帮助.麻烦动下手点下页面的广告吧, ...

  3. 深入理解Java执行时数据区

    前情回想 在本专栏的前12篇博客中. 我们主要大致介绍了什么是JVM, 而且具体介绍了class文件的格式. 对于深入理解Java, 或者深入理解运行于JVM上的其它语言, 深入理解class文件格式 ...

  4. html5--js函数在canvas中的应用

    html5--js函数在canvas中的应用 总结: 1.script中的函数写了要调用 2.rgb()这样的模式的色彩比较适合做变量 3.body的onload事件 4.带参函数 效果: 代码: & ...

  5. hdu 5023(线段树区间染色,统计区间内颜色个数)

    题目描述:区间染色问题,统计给定区间内有多少种颜色? 线段树模板的核心是对标记的处理 可以记下沿途经过的标记,到达目的节点之后一块算,也可以更新的时候直接更新到每一个节点 Lazy操作减少修改的次数( ...

  6. 关于在Eclipse上运行Hadoop程序的日志输出问题

    在安装由Eclipse-Hadoop-Plugin的Eclipse中, 可以直接运行Hadoop的MapReduce程序, 但是如果什么都不配置的话你发现Eclipse控制台没有任何日志输出, 这个问 ...

  7. IE6 浏览器常见兼容问题 大汇总(23个)[转载]

    IE6以及各个浏览器常见兼容问题 大汇总 综述:虽然说IE6在2014年4月将被停止支持,但是不得不说的是,IE6的市场并不会随着支持的停止而立刻消散下去,对于WEB前端开发工程师来说,兼容IE6 兼 ...

  8. bzoj 1923: [Sdoi2010]外星千足虫【高斯消元】

    裸的异或高斯消元 #include<iostream> #include<cstdio> using namespace std; const int N=2005; int ...

  9. bzoj 1609[Usaco2008 Feb]Eating Together麻烦的聚餐【dp】

    设up[i][j]为第i位升序为j的最小修改数,down为降序 #include<iostream> #include<stdio.h> using namespace std ...

  10. bzoj 1601: [Usaco2008 Oct]灌水【最小生成树】

    挺有意思的思路 如果不能自己打井,那么就是MST裸题了,考虑转换一下,自己打井就相当于连接一口虚拟的井(地下水?),所有井i到这口井的距离是w[i],这样把所有边排个序跑MST即可 #include& ...