码迷,mamicode.com
首页 > 移动开发 > 详细

iOS Developer Libray (中文版)-- About Objective-C

时间:2016-02-23 11:22:40      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更好,大家一起努力共同进入,有兴趣的同学可以一起学习。

另附word文档下载 

About Objective-C  关于 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系统上编程的主要语言(后面会简称OC),它是一种C的超集并且提供动态运行时(因为OC基于C,所以你可以理解为OC是别人写好的一个非常牛的函数库,当然事实并非如此简单),OC继承了C的语法、基本数据类型和流控制语句,并添加、定义了自己的语法、类和方法。在同时提供动态类型和绑定的同时,它还增加了对【对象图管理】和【对象字面量】语言级的支持,将许多任务推迟到运行时处理。

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.

这份文档介绍了OC语言,并且提供了大量的用法示例。你将学会怎么创建一个类去描述一个自定义对象、怎么使用一些Cocoa或者Cocoa Touch 提供的框架类工作。虽然这些框架类是独立于语言的,但是在使用OC编程和它们密切相关,而且一些语言特性依赖于这些类。

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.

当你创建一个OS X或者iOS应用时,你会花费大部分时间在对象上。这些对象是OC的类的实体,(类)一些由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.

如果你创建自己的类,首先要准备的是关于类的描述和公开接口的定义,接口的声明(interface)包括(公共)属性和方法列表。方法的声明定义了一个方法可接收到消息,和调用该方法时的参数类型。当然你还需要提供方法的实现(implementation),实现中包含该方法的可执行代码。

Relevant Chapters: Defining ClassesWorking with ObjectsEncapsulating 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.

相比较于在一个已存在的类的基础上创建一个全新的类去实现一个小的功能,在这个存在的类上添加类目是个更好的选择,你可以为任何类添加类目,包括你没有实现源码的类(别人写好的一些东西或者系统的一些会采取保护措施,可以引用但是看不到源码)比如:framework中的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.

如果你有类的源码,你可以使用延展去添加属性、或者修改已经存在的属性,延展通常被用来处理隐藏一些类内部的行为,或者用在一些自定义的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.

OC应用的很大一部分工作是重现别的应用发送的消息(可以简单地理解为交流吧)。通常,在类的接口中会明确声明这些消息,然是有时候,消息会被一系列的类使用,而不是某一个类。

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.

OC使用协议定义一组相关的方法,比如委托对象提供的协议,会声明该方法是可选的(optional)还是必须的(required)。任何遵守该协议的类,都需要实现协议中必须实现的(required)方法。

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.

在OC中很常见的一件事就是用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.

集合通常通常用一个类的实体表示,比如:NSArray、NSSet、NSDictionary,他们都是用来集合其他的OC对象的。

Relevant Chapters: Values and Collections

相关章节:值和集合

Blocks Simplify Common Tasks BLOCK代码块

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(代码块)是被引入C、OC、C++用来表示单元任务的一个语言特色,它将一块代码额捕获状态封装到一起,这让他看起来像是与其他编程语言完全隔绝开了。代码块通常用于处理一些通用事情,比如数据收集、分类和测试。同时他把使用GCD安排并发或者异步的计划工作变得简单。

Relevant Chapters: Working with Blocks

相关章节:使用BLOCK

Error Objects Are Used for Runtime Problems   NSError

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.

尽管OC包含的语法包含异常处理,但是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.

其他的所有错误—包括运行时问题,比如磁盘空间不足、网络无法使用,都是有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.

当你写OC代码时,你应该注意一些编码习惯。比如方法名:小写字母开头、使用驼峰命名法;像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 likeKey-Value Coding (KVC) or Key-Value Observing (KVO).

另外,如果你想使用OC语言的框架的优点,有一些约定是你必须遵守的;比如:属性访问方法,为了使用KVC或KVO你必须严格遵守语法规则。

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开发,在读该文档之前你应该先通读《 Start Developing iOS Apps Today or Start Developing Mac Apps Today  》,了解OS X、iOS应用开发的一般流程,另外,在度翁当之前你应该先熟悉XCode的基本使用。Xcode是用于OS X、iOS应用开发编辑器。你可以用它写代码,设计的应用,测试和调试你的应用。

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.

了解基本的面向对象的开发的知识,假设你至少有一丁点的面向对象的知识,尤其是OC中的面向对象。如果你真的一点也不了解,你应该先去读OC开发中的相关章节。

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.

本文档适用于Xcode4.4及以上版本,且开发在OS X10.7及以上版本,或者iOS5以上版本,更多关于Xcode的信息参阅:XCode概览,关于语言特性相关的信息,参阅:OC特性。

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.

OC使用引用计数决定对象的生命周期,大部分情况下,编译器的自动内存管理(ARC)功能替你处理这些事,如果你无法使ARC,或者需要维护使用手动内存管理(MRC)的老代码,参阅:高级内存管理指南。

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.

除了编译器,OC使用运行时机制实现动态语言和面向对象的特性,但是大部分时间你不用关心OC如何工作,就可以直接使用OC。参阅:OC编程指南 、 OC运行时参考。

iOS Developer Libray (中文版)-- About Objective-C

标签:

原文地址:http://www.cnblogs.com/kongkaikai/p/5209151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!