Skip to content

Commit

Permalink
Merge pull request oa414#6 from heistings/master
Browse files Browse the repository at this point in the history
第五章前面部分修正
  • Loading branch information
oa414 committed May 2, 2015
2 parents a9dbfe9 + b08e500 commit 10d6b1f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions translation/5-类.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
You should always prefix your class with **three** capital-case letters (two letters are reserved for Apple classes), this looking-weird practice is done to mitigate the notable absence of namespaces in our favorite language.
Some developers don't follow this practice for model objects (we observed this practice especially for Core Data objects), we advice to strictly follow this convention also in the case of Core Data objects because you might end up merging your Managed Object Model with other MOMs, maybe coming from third party library.
As you may already have noticed, in this book the class (but not only) prefix is `ZOC`.

类名应加上 **** 个大写字母作为前缀(两个字母的为 Apple 的类保留)。虽然这个规范看起来难看,但是这样做是为了减少 objective-c 没有命名空间所带来的问题。

你应该总是把你的类加上 **** 个字母的前缀(两个字母的为 Apple 的类保留)。这个难看的的实践是为了减少我们最喜欢的语言没有命名空间的影响
一些开发者在定义 Model 对象时并不遵循这个规范(对于 Core Data 对象,我们更应该遵循这个规范)。我们建议在定义 Core Data 对象时严格遵循这个约定,因为你最后可能把你的 Managed Object Model 和其他(第三方库)的 Managed Object Model 合并

一些开发者在 Model 对象中不使用这个实践(我们保留了这个实践,特别是 Core Data 对象),我们建议严格按照这个约定。即使在 Core Data对象中。因为你可能会把你的 Managed Object Model 用其他 MOM 管理,可能来自第三方库。你可能注意到了,这本书的类(但是不仅仅)前缀都是`ZOC`
你可能注意到了,这本书里的类的前缀(不仅仅)是`ZOC`

There is another good practice that you might want to follow while choosing the name for your classes: when you're creating a subclass, you should put the specifying name part between the class prefix and the superclass name. This is better explained with an example: if you have a class named `ZOCNetworkClient`, example of subclass name will be `ZOCTwitterNetworkClient` (note "Twitter" between "ZOC" and "NetworkClient"); or, following the same rule, a `UIViewController` subclass would `ZOCTimelineViewController`.

这里有另外一个你应该遵守来命名你的类的实践:当你创建一个子类的时候,你应该把它的特定的名字作为类前缀,把超类的名字放在中间。举个例子:如果你有一个 `ZOCNetworkClient` 类,子类的名字会是`ZOCTwitterNetworkClient` (注意 "Twitter" 在 "ZOC" 和 "NetworkClient" 之间); 按照这个约定, 一个`UIViewController` 的子类会是 `ZOCTimelineViewController`.
另一个类的命名规范:当你创建一个子类的时候,你应该把说明性的部分放在前缀和父类名的在中间。举个例子:如果你有一个 `ZOCNetworkClient` 类,子类的名字会是`ZOCTwitterNetworkClient` (注意 "Twitter" 在 "ZOC" 和 "NetworkClient" 之间); 按照这个约定, 一个`UIViewController` 的子类会是 `ZOCTimelineViewController`.


## Initializer and dealloc 初始化和释放
## Initializer and dealloc 初始化和 dealloc

The recommended way to organize the code is to have `dealloc` method placed at the top of the implementation (directly after the `@synthesize` and `@dynamic` statements) and the `init` should be placed directly below the `dealloc` methods of any class. In case of multiple initializer the designated initializer should be placed as first method, because is the one where most likely there is more logic, and the secondary initializers should follow.

推荐在有 `dealloc` 方法的代码里面,把它放在实现的顶部。(直接在 `@synthesize` 以及 `@dynamic` 之后) 并且 `init` 应该放在 `dealloc` 之后。在多个初始化方法的类中 designated initializer 应该放在第一个,因为这样逻辑性更好,并且 secondary initializer 在之后紧随。
推荐的代码组织方式:将 `dealloc` 方法放在实现文件的最前面(直接在 `@synthesize` 以及 `@dynamic` 之后)`init` 应该放在 `dealloc` 之后。如果有多个初始化方法, designated initializer 应该放在第一个,secondary initializer 在之后紧随,这样逻辑性更好

In these days with ARC, it is less likely that you will need to implement the dealloc method, but still the rationale is that by having the dealloc very close to your init methods, you are visually emphasizing the pairing between the methods. Usually in the dealloc method you will undo things that you have done in the init methods.

在有 ARC 的日子下,你几乎不需要实现 dealloc 方法,但是关于dealloc的基本原理和 init 方法很像。你应该在方法中进行显示地强调,同时在 dealloc 中,你需要取消执行在 init 方法做的事情
如今有了 ARCdealloc 方法几乎不需要实现,不过把 init dealloc 放在一起可以从视觉上强调它们是一对的。通常,在 init 方法中做的事情需要在 dealloc 方法中撤销


`init` methods should be structured like this:
Expand Down

0 comments on commit 10d6b1f

Please sign in to comment.