iOS Developer Libray (中文版)-- About Objective-C
该篇是我自己学习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 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.
相比较于在一个已存在的类的基础上创建一个全新的类去实现一个小的功能,在这个存在的类上添加类目是个更好的选择,你可以为任何类添加类目,包括你没有实现源码的类(别人写好的一些东西或者系统的一些会采取保护措施,可以引用但是看不到源码)比如: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的更多相关文章
- iOS Developer Libray (中文版)-- Defining Classes 定义类
该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更 ...
- iOS Developer Library地址
1. iOS Developer Library路径:https://developer.apple.com/library/ios/navigation/ 2. 百度搜索:iOS Developer ...
- 如何学习iOS开发?iOS Developer Library足矣!
记得上高中的时候,寄信请教二哥学习经验,二哥来信介绍学习经验说:资料书要快速阅读,把书上的题做完,然后再买几套资料书(习题集)继续练习. 这是二哥的经验,因为他自学能力强,可以消化多套资料书. 我仿照 ...
- iOS Developer:真机测试
如果出现ios development一项为灰色不可点击状态,苹果的说法是 如果您要为此电脑添加证书,请revoke以前的证书后添加,或者通过以前的mac导出证书 原文不记得了,大概这个意思,苹果不希 ...
- 【转】iOS Developer:真机测试
摘要 申请真机调试证书全过程,最新更新:2014-05-19 2014-10-16记:由于现在 itunes 更新变化较大,本文可能不能很好地解决您的问题,而我现在不负责公司的发布调试问题,暂未及时更 ...
- [Android开发教程]Android官网developer training中文版教程 - 1.1.1 创建一个Android项目
本系列持续更新中.转载请注明来源. 前言:近期打算系统学习一下Android开发,发现Android官网上的developer training也是个非常好的学习资料,于是想到一边学习一边写一个中文版 ...
- 作为一个iOS Developer 为什么我不用Swift?
1.开始 在去年这个时候接手了一个iOS项目,项目主用Swift语言进行开发,对于部分第三方Objective C开源库则使用bridge的方式进行调用 当时项目的规模大概是不超过15个页面,功能也比 ...
- 翻译Beginning iOS 7 Development中文版
不会iOS开发好像真的说只是去,来本中文版的Beginning iOS 7 Development吧. 看了Beginning iOS 7 Development这本书,感觉蛮不错的.全英文的,没有中 ...
- iOS资源大全中文版
我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-ios 就是 vsouza 发起维护的 iOS 资源列表,内容包括:框架.组件.测试.App ...
随机推荐
- Newtonsoft.Json 与 DataTable的相互转换
1.这里下载:http://www.newtonsoft.com/products/json/ 安装: 解压下载文件,得到Newtonsoft.Json.dll 在项目中添加引用 2.引入 ...
- android ScrollView 充满屏幕
android:fillViewport=true ScrollView下面的组件如果有android:layout_height="fill_parent"或android:la ...
- UVa 993: Product of digits
这道题很简单.先将N用2,3,5,7(即10以内的素数)分解因数(需要先特殊判断N不为1),然后将可以合并的因数合并(如2*2合并成4,)这样求得的结果位数会减少,大小肯定会小一些.具体实现见代码. ...
- Java基础知识强化之集合框架笔记17:List集合的特有的遍历功能
1. List集合的特有遍历功能: size()和 get()方法结合使用 2. 代码示例: package cn.itcast_03; import java.util.ArrayList; imp ...
- iOS UIKit:viewController之动画(5)
当弹出一个view controller时,UIKit提供了一些标准转换动画,并且也支持用户自定义的动画效果. 1 UIView动画 UIView是自带动画实现功能,其中有两种方式实现: ...
- 使用PowerShell读、写、删除注册表键值
访问注册表键值 在PowerShell中,用户可以通过类似于HKCU:(作为HKEY_CURRENT_USER)和HKLM:(代表HKEY_LOCAL_MATCHINE)的虚拟驱动器访问注册表键值. ...
- 启动hadoop时候报错:localhost: ssh: Could not resolve hostname localhost: Temporary failure in name resolution”
这个错误是由于配置文件没有配置好.解决方案如下: 1 打开profile文件 vim /etc/profile 2 在文件最后加入的内容应该如下(高亮的两句一般是大家缺少的): export JAVA ...
- typeerror $.ajax is not a function
在web开发中使用jQuery进行前端开发遇到这么个问题,纠结了很久终于解决了,下面说一下解决方法. 大家可以参照下面几种排查的方法. 1.首先检查是否引用jQuery的库. 2.页面如果使用的ifr ...
- CSS3渐变(Gradients)-线性渐变
CSS3渐变(Gradients)可以让你在两个或多个指定颜色之间显示平稳的过度,包括透明度. 以前,你必须使用图像来实现这些效果.但是,通过Css3渐变(Gradients),你可以减少下载的事件和 ...
- 利用bat批量执行脚本文件
1.读取目录文件 利用bat 的for命令读取中的sql文件 for /r %%c in (0*.sql) do echo %%c %%c 相当于变量 in() 中的为循环的范围 此句的作用是显示当前 ...