Introduction
ps:斜体表示翻译存在疑问
About Objective-C
关于Objective-C
Objective-C is the primary programming language you use when writing software for OS X and iOS. It’s a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime. Objective-C inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining classes and methods. It also adds language-level support for object graph management and object literals while providing dynamic typing and binding, deferring many responsibilities until runtime.
Objective-C是为OS X和iOS系统编写软件的首选语言。它是C语言的超集,在C语言的基础上提供了面向对象功能和动态的runtime机制。Objective-C继承了C语言的语法、原始数据类型、流程控制的同时,又增加了定义类和方法的语法。OC在底层支持类图和对象字面量,并提供动态类型和动态绑定功能,使许多工作得以在运行时进行。
理解:动态的runtime机制、类图、对象字面量、动态类型、动态绑定
At a Glance
概述
This document introduces the Objective-C language and offers extensive examples of its use. You’ll learn how to create your own classes describing custom objects and see how to work with some of the framework classes provided by Cocoa and Cocoa Touch. Although the framework classes are separate from the language, their use is tightly wound into coding with Objective-C and many language-level features rely on behavior offered by these classes.
此文档将介绍OC语言并提供大量的使用实例。你将了解到如何创建类来描述自定义对象,了解到如何使用Cocoa和Cocoa Touch提供的一些框架类工作。虽然这些框架类和OC语言是分离的,但它们的使用与OC的编码紧密相连,许多底层的特性依赖于这些类提供的行为。
理解:Cocoa、Cocoa Touch
An App Is Built from a Network of Objects
应用程序是一组对象的有机结合
When building apps for OS X or iOS, you’ll spend most of your time working with objects. Those objects are instances of Objective-C classes, some of which are provided for you by Cocoa or Cocoa Touch and some of which you’ll write yourself.
为OS X和iOS构建app时,我们将会花费大多数时间在对象上。这些对象是OC类的实例,其中的一部分是由Cocoa和Cocoa Touch提供的,一部分是由自己创建的。
If you’re writing your own class, start by providing a description of the class that details the intended public interface to instances of the class. This interface includes the public properties to encapsulate relevant data, along with a list of methods. Method declarations indicate the messages that an object can receive, and include information about the parameters required whenever the method is called. You’ll also provide a class implementation, which includes the executable code for each method declared in the interface.
如果你在写自己的类,应该从编写描述该类的接口文件开始,其应该显示使用该类的详细信息。 该接口文件应该包含封装数据的属性及方法列表。方法的声明表明了一个对象能够接收到的信息,以及该方法被调用时的参数信息。你也需要提供一个类的实现,包含了每个方法声明的具体实现代码。
Relevant Chapters: Defining Classes, Working with Objects, Encapsulating Data
相关章节:类的定义、使用对象、数据封装
Categories Extend Existing Classes
类别是对现有类的扩展
Rather than creating an entirely new class to provide minor additional capabilities over an existing class, it’s possible to define a category to add custom behavior to an existing class. You can use a category to add methods to any class, including classes for which you don’t have the original implementation source code, such as framework classes like NSString.
可以通过定义一个类别为一个现有的类增加自定义行为,而不必创建一个新的类。可以使用类别为任何的类增加方法,包括没有源代码的类,例如框架中的类NSString。
理解:Category的定义、使用
If you do have the original source code for a class, you can use a class extension to add new properties, or modify the attributes of existing properties. Class extensions are commonly used to hide private behavior for use either within a single source code file, or within the private implementation of a custom framework.
如果拥有类的源代码,那么就可以使用类扩展增加新属性,或者修改现有的类属性。类扩展通常用于隐藏私有属性或方法,要么是在一个源文件中,要么是在自定义的框架中。
理解:类扩展就是category?
Relevant Chapters: Customizing Existing Classes
相关章节:修改现有的类
Protocols Define Messaging Contracts
协议定义了信息发送的规则
The majority of work in an Objective-C app occurs as a result of objects sending messages to each other. Often, these messages are defined by the methods declared explicitly in a class interface. Sometimes, however, it is useful to be able to define a set of related methods that aren’t tied directly to a specific class.
OC的App中的主要工作机制是对象间彼此发送消息。通常,这些消息在类的接口文件中通过方法的声明明确表示出来。然而有时,它也会定义一系列不绑定任何具体类的方法。
Objective-C uses protocols to define a group of related methods, such as the methods an object might call on its delegate, which are either optional or required. Any class can indicate that it adopts a protocol, which means that it must also provide implementations for all of the required methods in the protocol.
OC使用协议定义一组相关的方法,例如一些对象可以委托调用这些方法 ,这些方法可以是必须实现的也可以是可以选择实现的。任何的类都可以表明它遵守某个协议,这意味着它必须实现协议中要求必须实现的方法。
Relevant Chapters: Working with Protocols
相关章节:使用协议
Values and Collections Are Often Represented as Objective-C Objects
数据值和集合通常使用OC对象表示
It’s common in Objective-C to use Cocoa or Cocoa Touch classes to represent values. The NSString class is used for strings of characters, the NSNumber class for different types of numbers such as integer or floating point, and the NSValue class for other values such as C structures. You can also use any of the primitive types defined by the C language, such as int, float or char.
通常,OC使用Cocoa或者Cocoa Touch的类来表示数据值。NSString类用来表示字符串,NSNumber类用来表示不同类型的数例如整形和浮点型,NSValue用来表示其他类型的数据值例如C结构体。你也可以使用C语言定义的原始数据类型,例如整形,浮点型或字符型。
Collections are usually represented as instances of one of the collection classes, such as NSArray, NSSet, or NSDictionary, which are each used to collect other Objective-C objects.
集合通常由集合类的实例表示,例如NSArray,NSSet,或者NSDictionary,他们被用来容纳其它OC对象。
理解:NSSet
Relevant Chapters: Values and Collections
相关章节:数据值和集合
Blocks Simplify Common Tasks
代码块简化通常的任务
Blocks are a language feature introduced to C, Objective-C and C++ to represent a unit of work; they encapsulate a block of code along with captured state, which makes them similar to closures in other programming languages. Blocks are often used to simplify common tasks such as collection enumeration, sorting and testing. They also make it easy to schedule tasks for concurrent or asynchronous execution using technologies like Grand Central Dispatch (GCD).
|