Objective-C Learn-Apple

Objective-C Learn 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語言,並增加了語法定義的類和方法。
它還增加了語言級的支持對象圖管理和對象文本,同時提供弱型別與強型別的語言支持,延遲執行許多責任,直到執行時期。
名詞解釋:
>
runtime
執行時期(Run time)在電腦科學中代表了一個電腦程式運作、執行的時期,從開始執行到終止執行。與執行時期相對的其他時期包括:
設計時期(design time)、編譯時期(compile time)、鏈結時期(link time)、與載入時期(load time)。
而執行環境是一種為正在執行的程式或程式提供軟體服務的虛擬機械環境。它有可能是由作業系統自行提供,或由執行此程式的母程式提供。

強型別:一種需宣告明確的撰寫方式
int a= 0;
弱型別:一種宣告不明確的撰寫方式,由資料內容判斷型別
id a = 1;
id a = @"String";
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.
首先
本文件介紹了Objective-C語言,並提供其使用的大量的範例。您將學習如何創建自定義的描述對象的類和看到如何用一些由Cocoa 和Cocoa Touch所提供的框架類的工作。雖然框架類是從語言獨立的,它們的使用被緊緊纏繞與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. 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.
一個APP是建立在網路或者物件上的
建立用於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
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.
Relevant Chapters: Customizing Existing Classes
Protocols Define Messaging Contracts
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的應用程序的大部分工作發生是由於將消息發送到對方的對象的結果。通常情況下,這些消息是利用方法定義在明確的類別interface上。有時候,然而它可以被定義在相關的方法而不需要指向特定的類別。
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使用protocols 來定義一組相關的方法,如一個對象可能在其delegate調用的方法,其或者是可選的或必需的。任何類可以表明它採用一個protocols ,這意味著它提供implementations 給所有需要的方法在protocol。
Relevant Chapters: Working with Protocols
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. 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.
相關章節:與合作協議
值和集合通常表示為Objective-C的對象
這是在Objective-C使用Cocoa 或Cocoa Touch類來表示共同的價值觀念。 NSString類用於字符的字符串,則NSNumber的類不同類型的數字,如整數或浮點數,以及如C結構等值NSValue類。您也可以使用任何由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的物件
Relevant Chapters: Values and Collections
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. 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).
有關章節:價值和收藏
Blocks 簡化Common Tasks
Blocks 引入到C,Objective-C和C ++來表示一個工作單元的語言特徵;它們封裝的代碼塊繳獲的狀態,這使得它們類似於其他編程語言關閉一起。Blocks 通常被用來簡化共同的任務,例如收集枚舉,排序和測試。他們也可以很容易地安排像大中央調度(GCD)並行或異步執行使用的技術任務。
Relevant Chapters: Working with Blocks
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.
有關章節:使用Blocks工作
錯誤對象用於運行問題
雖然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. 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.
所有其他錯誤,包括運行時的問題,如運行的磁盤空間不足或不能夠訪問Web服務,通過NSError類的實例表示。您的應用程序應該計劃的錯誤,並決定如何最好地處理它們才能呈現最佳的用戶體驗出問題的時候。
Relevant Chapters: Dealing with Errors
Objective-C Code Follows Established Conventions
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.
相關章節:處理錯誤
Objective-C的代碼遵循既定習俗
當寫Objective-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).
此外,還有如果你想利用語言或框架的功能優勢還需要幾個約定。屬性訪問方法,例如,必須遵循嚴格的命名約定,以便像鍵 - 值編碼(KVC)或鍵 - 值觀察(志願)的技術工作。
Relevant Chapters: 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開發,您應該通過閱讀開始開發iOS應用今天或開始閱讀本文檔前,開發Mac應用程序如今,能為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的情況下適用,但它假定您至少有一個最小的熟悉基本的面向對象的概念。如果你不熟悉這些概念,你應該閱讀在Objective-C編程概念的相關章節。
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.
也可以看看
本文檔中的內容適用於Xcode的4.4或更高版本,並假設你的目標或者OS X v10.7或更高版本,或iOS5或更高版本。有關Xcode的更多內容,請參閱Xcode的概述。有關語言功能可用性的信息,請參閱Objective-C的功能可用性指標。
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.
Objective-C的應用程序使用引用計數來確定對象的生命週期。在大多數情況下,編譯器的自動引用計數(ARC)功能需要照顧這個要求。如果您無法採取ARC的優勢,還是需要轉換或維持手動管理對象的內存遺留代碼,您應該閱讀高級內存管理編程指南。
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.
除了編譯器,Objective-C語言使用一個運行時系統,以使其充滿活力和面向對象的特性。雖然你通常不需要擔心Objective-C如何“工作”,有可能直接與該運行系統進行交互,如Objective-C的運行時編程指南和Objective-C運行時參考描述。

Comments

Popular posts from this blog

MEGA 暫存檔案刪除

XAMPP 使用多PORT來執行不同網頁

IOS GCD多執行緒