Programming With Objective-C---- Introduction ---- Objective-C 学习(一)

 

About 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编程语言的超集,并提供了面向对象的功能以及一个动态运行环境。 Objective-C继承了C语言的语法,原始类型以及流程控制语句, 并添加了定义类和方法的语法。另外,它还添加了对对象图管理和对象文本(object literals)语言层上的支持,同时提供了动态类型(dynamic typing) 和 绑定(binding),以及推迟很多责任,直到运行时(deferring many responsibilities until 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.

本文档介绍了Objecttive-C并提供了大面积的使用实例。 你将学习如何创建自己的类来描述自定义对象以及如何使用一些由Cocoa 和 Cocoa Touch提供的框架类。尽管框架被从语言分离出来,但是它们跟Objective-C编程密切相关。并且很多语言层的功能依赖这些类提供的行为方法。

An App Is Built from a Network of Objects

1.一个 应用程序就是一个各种对象组成的网络

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 应用程序时,你大部分时间都是跟各种对象一起工作的。 那些对象都是Objective-C类的实例,它们其中一些是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

相关章节:Defining Classes, Working with Objects, Encapsulating Data

 

Categories Extend Existing Classes

2. 分类用于扩展现有的类

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.

与其创建一整个新类来实现一个现有类的次要附加功能,不如在现有类的基础上定义一个category(类别)来添加自定义行为。 你可以使用一个category来给任何方法添加各种方法, 即使是那些你没有初始源代码的类,比如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.

如果你确实有一个类的初始源代码,你可以使用一个类的扩展来添加新的特性,或者修改已存在特性的属性。类扩展通常用在一个单一的源代码文件或者一个自定义框架的私有实现中,用来隐藏私有行为方法。

Relevant Chapters: Customizing Existing Classes

相关章节:Customizing Existing Classes

 

Protocols Define Messaging Contracts

3. 协议用来定义各种消息合同

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.

在一个Object-C应用中发生的大多数工作都会导致各个对象之间互相传递各种消息。 通常,这些消息有在一个类的接口中明确定义。有时候,定义一系列不跟一个特定类绑定的相关方法也很有用。

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.

Object-C使用各种协议来定义一组相关的方法,比如一个对象可能在它的代理调用的各种方法,它们也许是可选的,也许是必须的。 任何类如果采用了一个协议,就说明它必须实现该协议中所有的必选(required)方法。

Relevant Chapters: Working with Protocols

相关章节: Working with Protocols

Values and Collections Are Often Represented as Objective-C Objects

4. 值和集合常被看做是Objective-C对象

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 intfloat or char.

在Object-C里用Cocoa 或 Cocoa Touch 类表示各种值很常见。 NSString类被用于字符串; NSNumber 类则用于不同类型的数字,比如整型或浮点型;NSValue类则用于表示其它各种各样的值,比如C语言结构等。你也可以使用任何C语言中定义的原始类型,比如int, float 或者 char.

Collections are usually represented as instances of one of the collection classes, such as NSArrayNSSet, or NSDictionary, which are each used to collect other Objective-C objects.

集合则通常由collection类的一个实例表示,比如NSArray, NSSet, 或者NSDictionary,它们都用于收集其它Objective-C对象。

Relevant Chapters: Values and Collections

相关章节:Values and Collections

 

Blocks Simplify Common Tasks

5. 块简化了通用任务

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).

块(block)是一个语言功能,在C, Objective-C, 和 C++中用来表示一个代码单元;它们把代码和捕捉的状态封装在一个块里,使得它们跟其它编程语言的closure(闭包)很类似。块通常用于简化通用任务,比如集合枚举,排序和测试等。它们还让使用类似Grand Central Dispath(GCD)技术的并发和异步等计划任务变得简单。

Relevant Chapters: Working with Blocks

相关章节:Working with Blocks

 

Error Objects Are Used for Runtime Problems

6. Error对象用于解决运行时问题

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.

尽管Objective-C包含了异常处理的语法,但是Cocoa 和Cocoa Touch的异常处理只用于一个应用程序发布前需要解决的编码错误(比如数组越界)。

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 theNSError 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.

所有其它错误---包括运行时错误,比如磁盘空间不足或无法访问一个网络服务等---是由NSError类的实例管理的。当错误发生时,为了提供尽可能最好的用户体验,你的应用程序应该为各种错误做好计划并决定如何最完美的处理它们。

Relevant Chapters: Dealing with Errors

相关章节:Dealing with Errors

 

Objective-C Code Follows Established Conventions

7. Objective-C代码遵循既定习俗

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.

当你编写Object-C代码时,你的心中应该牢记一些既定习俗。 对于方法名称,举个例子,以小写字母开头,多个单词以首字母大写相连,如doSomething 或者 doSomethingElse;不单大写很重要,你还应该尽可能地确保代码的可读性,这意味着方法名称应该是令人印象深刻但是却不会太长。

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).

另外,如果你希望充分利用语言或框架,还有一些习俗是必须的。 比如,为了能跟像 Key-Value Coding (KVC) 或 Key-Value Observing (KVO)之类的技术一起工作,特性访问器方法必须遵循严格的命名命名习俗。

Relevant Chapters: Conventions

相关章节:Conventions

 

Prerequisites

二、预备知识

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.

如果你是刚开始OS X 或 iOS开发, 你应该在读本文档之前先阅读 Start Developing iOS Apps Today 或 Start Developing Mac Apps Today ,这样就可以读iOS 和 OS X开发过程有个大概的了解。 另外,你应该在练习本文档大多数章节后的习题之前先熟悉Xcode。 Xcode是构建iOS 和 OS X应用程序的IDE;你将用它编写代码,设计应用程序的用户界面,测试你的应用程序,以及调试各种问题。

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.

尽管最好对C或某种基于C语言的语言有一定了解, 比如Java 或 C#,本文档确实包含了一些基本的C语言功能,比如流程控制语句。 另外如果你有别的高层次编程语言方面的知识,比如Ruby 或 Python,你应该能够跟上本文档的内容。

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.

面向对象贯穿Objective-C的上下文,如果你不熟悉基本的面向对象等概念,你应该阅读Concepts in Objective-C Programming中的相关章节。

See Also

三、参见

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 andObjective-C Runtime Reference.

 

你可能感兴趣的:(Programming With Objective-C---- Introduction ---- Objective-C 学习(一))