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语言的超集。objective - C继承了C语言的语法、基本类型和流控制语句,并增加了定义类和方法的语法。它还增加了language-level用以支持对象图形管理和对象文本,同时提供动态类型和绑定,延迟许多责任到运行时执行。
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.
本文介绍了objective - c语言,并且提供了大量的例子。您将学习如何创建自己的类,描述的自定义对象,看看如何使用一些Cocoa和Cocoa Touch提供的框架类。虽然框架类是独立于语言的,但是他们的使用是与objective - c紧紧的联系在一起的,许多语言级功能依赖于这些类提供的方法。
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.
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.
在构建OS X或iOS应用程序时,你会花大部分时间和对象一起工作。这些对象是objective - c类的对象实例,其中一些是Cocoa和Cocoa Touch为您提供的,其中一些你会自己写。
如果你编写自己的类,首先提供的是类的描述(公共接口的类的实例的细节)。这个接口包含了公共属性,封装相关数据,以及方法的列表。方法声明显示对象可以接收的消息,包括无论何时调用此方法所需的参数信息。您还将提供一个类实现,其中包括在接口中声明的每个方法的可执行代码。
Relevant Chapters: Defining Classes, Working with Objects, Encapsulating Data
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
.
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.
在现有类提供较小的额外功能,与其创建一个全新的类覆盖它,你可以定义一个 类别 添加自定义行为到现有类。您可以使用一个类别添加任何类的方法,包括那些你没有原始实现源代码的类,例如像NSString框架类。
如果你有原始类的源代码,您可以使用类扩展来添加新属性,或修改现有属性的属性。类扩展通常用来在一个源代码文件中隐藏私有行为,或在自定义框架中的私有实现。
Relevant Chapters: Customizing Existing Classes
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.
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.
在一个objective - c应用程序中,大部分的工作发生于对象之间发送消息。通常,这些消息定义在一个显式声明的类方法的接口中。然而有时,不直接绑定到一个特定的类,而是定义一组相关的方法是个好办法。
objective - c使用 协议 来定义一组相关的方法,如此以来,方法对象会调用它委托,委托有可选的或者是必需的。任何类都可以表明采用一个协议,这意味着它还必须提供协议中所有必需实现的方法。
Relevant Chapters: Working with Protocols
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
.
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.
在objective - c中,普遍使用Cocoa和Cocoa Touch类来表示值。NSString类用于字符串的字符,NSNumber类用于不同类型的数据,如整数或浮点,NSValue类用于实现其他值,如C结构体。您还可以使用C语言中任何原始类型的定义,如int,float或char。
集合通常表示为集合类的实例,如NSArray,NSSet,或NSDictionary,每个都用于收集其他objective - c的对象。
Relevant Chapters: Values and Collections
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).
Relevant Chapters: Working with Blocks
Although Objective-C includes syntax for exception handling, Cocoa and Cocoa Touch use exceptions only for programming errors (such as out of bounds array access), which should be fixed before an app is shipped.
All other errors—including runtime problems such as running out of disk space or not being able to access a web service—are represented by instances of the NSError
class. Your app should plan for errors and decide how best to handle them in order to present the best possible user experience when something goes wrong.
虽然objective - c语法包括异常处理,但是Cocoa和Cocoa Touch类使用异常只针对编程错误(如数组界外访问),这些错误应该在应用产品上架前处理好
所有其他的错误--包括运行时耗尽磁盘空间或无法访问web服务器等问题是由NSError类的实例表示的。你的应用程序应该尽量预见可能出现的错误并决定如何最好地处理它们,这样才会有好的用户体验。
Relevant Chapters: Dealing with Errors
When writing Objective-C code, you should keep in mind a number of established coding conventions. Method names, for example, start with a lowercase letter and use camel case for multiple words; for example, doSomething
or doSomethingElse
. It’s not just the capitalization that’s important, though; you should also make sure that your code is as readable as possible, which means that method names should be expressive, but not too verbose.
In addition, there are a few conventions that are required if you wish to take advantage of language or framework features. Property accessor methods, for example, must follow strict naming conventions in order to work with technologies like Key-Value Coding (KVC) or Key-Value Observing (KVO).
当编写objective - c代码时,你应该记住一些已经建立的编码惯例。例如方法名称,以小写字母开头,并使用驼峰表示法,例如,doSomething或doSomethingElse。不仅仅大写是重要的,您还应该确保您代码的可读性,这意味着你的方法名应该富有表现力,但不要太冗长。
此外,如果你想利用语言或框架的特性,还有一些必要的约定。例如属性访问器方法,必须遵循严格的命名约定来处理键值编码等技术(KVC)或键-值观察(KVO)。
Relevant Chapters: Conventions
If you are new to OS X or iOS development, you should read through Start Developing iOS Apps Today or Start Developing Mac Apps Today before reading this document, to get a general overview of the application development process for iOS and OS X. Additionally, you should become familiar with Xcode before trying to follow the exercises at the end of most chapters in this document. Xcode is the IDE used to build apps for iOS and OS X; you’ll use it to write your code, design your app's user interface, test your application, and debug any problems.
Although it’s preferable to have some familiarity with C or one of the C-based languages such as Java or C#, this document does include inline examples of basic C language features such as flow control statements. If you have knowledge of another higher-level programming language, such as Ruby or Python, you should be able to follow the content.
Reasonable coverage is given to general object-oriented programming principles, particularly as they apply in the context of Objective-C, but it is assumed that you have at least a minimal familiarity with basic object-oriented concepts. If you’re not familiar with these concepts, you should read the relevant chapters in Concepts in Objective-C Programming.
如果你是OS X或iOS开发新手,你应该在读本文档之前阅读Start Developing iOS Apps Today或Start Developing Mac Apps Today 对iOS和OS X开发有一个概览。另外,你应该在试图做大多数章节的练习之前熟悉Xcode。Xcode 是用于构建为iOS和mac OS X应用程序的IDE(集成开发环境);你会用它来编写代码,设计你的应用程序的用户界面,测试您的应用程序,调试问题。
虽然有一些熟悉C或一个基于C的语言(如Java或c#)更好,本文档包括内联的例子基本的C语言特性,比如流控制语句。如果你有更高级的编程语言的知识,例如Ruby或Python,您应该能够跟上OC 的学习。
The content in this document applies to Xcode 4.4 or later and assumes you are targeting either OS X v10.7 or later, or iOS 5 or later. For more information about Xcode, see Xcode Overview. For information on language feature availability, see Objective-C Feature Availability Index.
Objective-C apps use reference counting to determine the lifetime of objects. For the most part, the Automatic Reference Counting (ARC) feature of the compiler takes care of this for you. If you are unable to take advantage of ARC, or need to convert or maintain legacy code that manages an object’s memory manually, you should read Advanced Memory Management Programming Guide.
In addition to the compiler, the Objective-C language uses a runtime system to enable its dynamic and object-oriented features. Although you don’t usually need to worry about how Objective-C “works,” it’s possible to interact directly with this runtime system, as described by Objective-C Runtime Programming Guide and Objective-C Runtime Reference.
本文档的内容适用于Xcode 4.4或更高版本,假定您针对OS X v10.7或更高版本,或者iOS 5或更高版本。关于Xcode的更多信息,请参见Xcode Overview。语言功能的可用性信息,请参阅Objective-C Feature Availability Index。
objective - c应用程序使用引用计数来确定对象的生命周期。在大多数情况下,自动引用计数(ARC),这个编译器特性为您的程序负责。如果你不能利用ARC,或需要转换或者维护手动管理对象内存的遗留代码,你应该阅读Advanced Memory Management Programming Guide。