About Objective-C

Objective-C is the primary programming language you use when writing software for OS X and iOS.

Objective-C是在爲OS X和iOS編寫軟件時使用的主要編程語言。

It’s a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime. 
它是C編程語言的超集,並提供面向對象的功能和動態運行時。

Objective-C inherits the syntax, primitive types, and flow control statements of C and adds syntax for defining classes and methods.
Objective-C繼承了C的語法,原始類型和流控制語句,並添加了用於定義類和方法的語法。

It also adds language-level support for object graph management and object literals while providing dynamic typing and binding, deferring many responsibilities until runtime.
它還提供了對對象圖管理和對象文字的語言級別支持,同時提供了動態類型和綁定,將許多職責推遲到運行時。

At a Glance

This document introduces the Objective-C language and offers extensive examples of its use.
本文檔介紹了Objective-C語言,並提供了其用法的廣泛示例。

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.
您將學習如何創建自己的描述自定義對象的類,並瞭解如何使用Cocoa和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進行編碼的過程中,許多語言級別的功能都依賴於這些類提供的行爲。

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. 
在爲OS X或iOS構建應用程序時,您將花費大部分時間來處理對象。

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.
這些對象是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.
您還將提供一個類實現,其中包括接口中聲明的每個方法的可執行代碼。

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之類的框架類。

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.
類擴展通常用於隱藏私有行爲,以便在單個源代碼文件中或在自定義框架的私有實現中使用。

Protocols Define Messaging Contracts

The majority of work in an Objective-C app occurs as a result of objects sending messages to each other. 
Objective-C應用程序中的大部分工作是對象之間相互發送消息的結果。

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. 
Objective-C使用協議定義一組相關方法,例如對象可能在其委託上調用的方法,這些方法是可選的或必需的。

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.
任何類都可以表明它採用了協議,這意味着它還必須提供協議中所有必需方法的實現。

Values and Collections Are Often Represented as Objective-C Objects

It’s common in Objective-C to use Cocoa or Cocoa Touch classes to represent values.
在Objective-C中,通常使用Cocoa或Cocoa Touch類來表示值。

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. 
NSString類用於字符串,NSNumber類用於不同類型的數字,例如整數或浮點數,NSValue類用於其他值,例如C結構。

You can also use any of the primitive types defined by the C language, such as int, float or char.
您還可以使用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.
集合通常表示爲集合類之一(例如NSArray,NSSet或NSDictionary)的實例,它們各自用於收集其他Objective-C對象。

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.
塊是C,Objective-C和C ++中引入的一種語言功能,代表一個工作單元。它們封裝了代碼塊以及捕獲的狀態,這使其類似於其他編程語言中的閉包。

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).
它們還使您可以使用諸如Grand Central Dispatch(GCD)之類的技術輕鬆地調度任務以進行併發或異步執行。

Error Objects Are Used for Runtime Problems

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 the NSError class. 
所有其他錯誤(包括運行時問題,例如磁盤空間不足或無法訪問Web服務)均由NSError類的實例表示。

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 Code Follows Established Conventions

When writing Objective-C code, you should keep in mind a number of established coding conventions. 
在編寫Objective-C代碼時,應牢記許多已建立的編碼約定。

Method names, for example, start with a lowercase letter and use camel case for multiple words; for example, doSomething or doSomethingElse. 
例如,方法名稱以小寫字母開頭,多個單詞使用駝峯大寫;例如,doSomething或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).
例如,屬性訪問器方法必須遵循嚴格的命名約定,才能與鍵值編碼(KVC)或鍵值觀察(KVO)之類的技術一起使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章