Programming With Objective-C---- Introduction ---- Objective-C 学习(一)

   2023-02-09 学习力0
核心提示:a:link { color: rgba(88, 114, 210, 1); text-decoration: none }a:visited { color: rgba(88, 114, 210, 1); text-decoration: none }a:hover { color: rgba(173, 189, 248, 1); text-decoration: none }a:active { color: rgba(0, 0, 255, 1); text-decora

 

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语言的语法,原始类型以及流程控制语句, 并添加了定义类和方法的语法。另外,它还添加了对对象图管理和对象文本(object literals)语言层上的支持,同时提供了动态类型(dynamic typing) 和 绑定(binding),以及推迟很多责任,直到运行时(deferring many responsibilities until runtime)。

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.

本文档介绍了Objecttive-C并提供了大面积的使用实例。 你将学习如何创建自己的类来描述自定义对象以及如何使用一些由Cocoa 和 Cocoa Touch提供的框架类。尽管框架被从语言分离出来,但是它们跟Objective-C编程密切相关。并且很多语言层的功能依赖这些类提供的行为方法。

An App Is Built from a Network of Objects

1.一个 应用程序就是一个各种对象组成的网络

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 应用程序时,你大部分时间都是跟各种对象一起工作的。 那些对象都是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 ClassesWorking with ObjectsEncapsulating Data

Encapsulating Data

 

Categories Extend Existing Classes

2. 分类用于扩展现有的类

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.

与其创建一整个新类来实现一个现有类的次要附加功能,不如在现有类的基础上定义一个category(类别)来添加自定义行为。 你可以使用一个category来给任何方法添加各种方法, 即使是那些你没有初始源代码的类,比如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

Customizing Existing Classes

 

Protocols Define Messaging Contracts

3. 协议用来定义各种消息合同

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.

在一个Object-C应用中发生的大多数工作都会导致各个对象之间互相传递各种消息。 通常,这些消息有在一个类的接口中明确定义。有时候,定义一系列不跟一个特定类绑定的相关方法也很有用。

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.

Object-C使用各种协议来定义一组相关的方法,比如一个对象可能在它的代理调用的各种方法,它们也许是可选的,也许是必须的。 任何类如果采用了一个协议,就说明它必须实现该协议中所有的必选(required)方法。

Relevant Chapters: Working with Protocols

Working with Protocols

Values and Collections Are Often Represented as Objective-C Objects

4. 值和集合常被看做是Objective-C对象

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 intfloat or char.

在Object-C里用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 NSArrayNSSet, or NSDictionary, which are each used to collect other Objective-C objects.

集合则通常由collection类的一个实例表示,比如NSArray, NSSet, 或者NSDictionary,它们都用于收集其它Objective-C对象。

Relevant Chapters: Values and Collections

Values and Collections

 

Blocks Simplify Common Tasks

5. 块简化了通用任务

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).

块(block)是一个语言功能,在C, Objective-C, 和 C++中用来表示一个代码单元;它们把代码和捕捉的状态封装在一个块里,使得它们跟其它编程语言的closure(闭包)很类似。块通常用于简化通用任务,比如集合枚举,排序和测试等。它们还让使用类似Grand Central Dispath(GCD)技术的并发和异步等计划任务变得简单。

Relevant Chapters: Working with Blocks

Working with Blocks

 

Error Objects Are Used for Runtime Problems

6. Error对象用于解决运行时问题

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 theNSError 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

Dealing with Errors

 

Objective-C Code Follows Established Conventions

7. Objective-C代码遵循既定习俗

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.

当你编写Object-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).

Key-Value Observing (KVO)之类的技术一起工作,特性访问器方法必须遵循严格的命名命名习俗。

Relevant Chapters: Conventions

相关章节: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.

Start Developing iOS Apps Today 或 Start Developing Mac Apps Today ,这样就可以读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.

Concepts in Objective-C Programming中的相关章节。

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.

 

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.

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 andObjective-C Runtime Reference.

Next

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • 《黑马程序员》 category分类的使用(Objective
    分类的作用:在不改变原来类的基础上,可以给类增加一些方法。使用注意 : ①  分类只能增加方法,不可以增加成员变量                ②  分类的方法在实现中可以访问成员变量,不过成员变量必须手动实现。               
    03-16
  • Objective-C学习—UIScrollView控件使用
    Objective-C学习—UIScrollView控件使用
    一、知识点简单介绍1.UIScrollView控件是什么?(1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限(2)当展⽰示的内容较多,超出⼀一个屏幕时,⽤用户可通过滚动⼿手势来查看屏幕以外的内容(3)普通的UIView不具备滚动功能
    03-16
  • Objective-C Runtime(转)
    博主地址: http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/曾经觉得Objc特别方便上手,面对着 Cocoa 中大量 API,只知道简单的查文档和调用。还记得初学 Objective-C 时把[receiver message]当成简单的方法调用,而无视了“发送消息”这句话
    03-08
  • iOS扩展——Objective-C开发编程规范 简单io扩展实验代码
    iOS扩展——Objective-C开发编程规范 简单io扩
      最近准备开始系统学习一个完整项目的开发流程和思路,在此之前,我们需要对iOS的开发变成规范进行更系统和详尽的学习,随意对编程规范进行了整理和学习。本文内容主要转载自:Objective-C-Coding-Guidelines-In-Chinese  此外,这篇文章所说的一些常见
    03-08
  • Objective-C利用协议实现回调函数
    实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下:  实现的代码如下:定义协议:#import UIKit/UIKit.h @protocol NoteDelegate //回调函数 -(void)messageCallBack:(NSString *)string; @end 调用协议:#impor
    02-10
  • 刨根问底Objective-C Runtime(1)- Self & S
      刨根问底Objective-C Runtime(1)- SelfSuper - Chun Tips专注iOS开发 刨根问底Objective-C Runtime(1)- SelfSuper前言关于Objective-C Runtime一篇好的文档 : Understanding the Objective-C Runtime译文地址为: http://blog.cocoabit.com/blog/2
    02-10
  • Objective-C利用协议实现回调函数
    实现一个显示文字为测试的视图,然后经过3秒钟测试文字变为回调函数文字。相应的截图如下: 实现的代码如下:定义协议:#import UIKit/UIKit.h @protocol NoteDelegate//回调函数-(void)messageCallBack:(NSString *)string;@end  调用协议:#import Founda
    02-10
  • Objective-c开发中混合使用ARC
    首选“Compile Sources”的位置:选中工程-TARGETS-相应的target然后选中右侧的“Build Phases”,向下就找到“Compile Sources”了。如何在未使用arc的工程中引入一个使用了arc特性的文件:对相应的文件添加:-fobjc-arc参数如何在arc工程中引用未使用arc的文件
    02-10
  • Objective C运行时(runtime)技术总结,好强大
    前言:         Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表、属性列表、变量列表,修改方法、属性,增加方法,属性等等,本文对相关的几个要点做了一个小结。目录:(1)使用class_replaceMethod/cla
    02-09
  • 设计模式之开放-封闭原则(引申出Objective-C中
    开放封闭原则(OCP原则The Open-Closed Principle)是面向对象的核心设计所在。它是说,软件开发实体(类、模块、函数等)应该可以扩展,但是不能修改。这个原则有两个特征,一个是说“对于扩展是开放的”,另一个是说“对于更改是封闭的”。我们在编写任何ap
    02-09
点击排行