首頁>技術>

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)之類的技術一起使用。

9
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 阿里P8純手寫SQL文件:收穫不止SQL最佳化抓住SQL的本質