標籤:
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 int
, float
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 NSArray
, NSSet
, 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.
Next
Programming With Objective-C---- Introduction ---- Objective-C 學習(一)