As the name implies, object-oriented programs are built around objects. An object associates data with the particular operations that can use or affect that data. Objective-C provides a data type to identify an object variable without specifying a particular class of the object.
顾名思义,面向对象程序是围绕对象构建的。对象将数据与可以使用或影响该数据的特定操作相关联。Objective-C提供了一种数据类型来识别对象变量,而不需要指定对象的特定类。
An object associates data with the particular operations that can use or affect that data. In Objective-C, these operations are known as the object’s methods; the data they affect are its instance variables (in other environments they may be referred to as ivars or member variables). In essence, an object bundles a data structure (instance variables) and a group of procedures (methods) into a self-contained programming unit.
对象将数据与可以使用或影响该数据的特定操作相关联。在Objective-C中,这些操作被称为对象的方法;它们影响的数据是它的实例变量(在其他环境中,它们可能被称为ivars或成员变量)。本质上,对象将数据结构(实例变量)和一组过程(方法)捆绑到一个自包含的编程单元中。
In Objective-C, an object’s instance variables are internal to the object; generally, you get access to an object’s state only through the object’s methods (you can specify whether subclasses or other objects can access instance variables directly by using scope directives). For others to find out something about an object, there has to be a method to supply the information. For example, a rectangle would have methods that reveal its size and position.
在Objective-C中,对象的实例变量是对象的内部变量;通常,您只能通过对象的方法访问对象的状态(您可以指定子类或其他对象是否可以使用范围指令直接访问实例变量)。对于其他人来说,要找到关于对象的信息,必须有一个方法来提供信息。例如,矩形将具有显示其大小和位置的方法。
Moreover, an object sees only the methods that were designed for it; it can’t mistakenly perform methods intended for other types of objects. Just as a C function protects its local variables, hiding them from the rest of the program, an object hides both its instance variables and its method implementations.
此外,一个对象只能看到为它设计的方法;它不能错误地执行针对其他类型对象的方法。就像C函数保护它的局部变量,不让它们被程序的其他部分看到一样,对象也隐藏了它的实例变量和方法实现。
In Objective-C, object identifiers are of a distinct data type: id. This type is the general type for any kind of object regardless of class and can be used for instances of a class and for class objects themselves.
在Objective-C中,对象标识符是一种不同的数据类型:id。这种类型是任何类型对象的通用类型,不管类是什么,它都可以用于类的实例和类对象本身。
id anObject;
For the object-oriented constructs of Objective-C, such as method return values, id replaces int as the default data type. (For strictly C constructs, such as function return values, int remains the default type.) The keyword nil is defined as a null object, an id with a value of 0. id, nil, and the other basic types of Objective-C are defined in the header file objc/objc.h. id is defined as pointer to an object data structure:
对于Objective-C的面向对象结构,例如方法返回值,id替换int作为默认数据类型。(对于严格的C结构,比如函数返回值,int仍然是默认类型。) 关键字nil被定义为一个空对象,一个值为0的id。id、nil和Objective-C的其他基本类型在头文件objc/objc.h中定义。id定义为指向对象数据结构的指针:
typedef struct objc_object {
Class isa;
} *id;
Every object thus has an isa variable that tells it of what class it is an instance. Since the Class type is itself defined as a pointer:
因此,每个对象都有一个isa变量,它告诉对象它是什么类的实例。因为类类型本身定义为指针:
typedef struct objc_class *Class;
the isa variable is frequently referred to as the “isa pointer.”
isa变量经常被称为“isa指针”。
The id type is completely nonrestrictive. By itself, it yields no information about an object, except that it is an object. At some point, a program typically needs to find more specific information about the objects it contains. Since the id type designator can’t supply this specific information to the compiler, each object has to be able to supply it at runtime.
id类型完全是非限制性的。它本身不会产生关于对象的任何信息,除非它是一个对象。在某个时候,程序通常需要找到关于它包含的对象的更具体的信息。由于id类型指示符不能向编译器提供此特定信息,因此每个对象必须能够在运行时提供该信息。
The isa instance variable identifies the object’s class—what kind of object it is. Objects with the same behavior (methods) and the same kinds of data (instance variables) are members of the same class.
isa实例变量标识对象的类——它是什么类型的对象。具有相同行为(方法)和相同类型数据(实例变量)的对象是同一个类的成员。
Objects are thus dynamically typed at runtime. Whenever it needs to, the runtime system can find the exact class that an object belongs to, just by asking the object. Dynamic typing in Objective-C serves as the foundation for dynamic binding, discussed later.
因此,对象在运行时是动态类型的。只要需要,运行时系统就可以通过询问对象找到对象所属的确切类。Objective-C中的动态类型是动态绑定的基础,稍后将对此进行讨论。
The isa variable also enables objects to perform introspection—to find out about themselves (or other objects). The compiler records information about class definitions in data structures for the runtime system to use. The functions of the runtime system use isa to find this information at runtime. Using the runtime system, you can, for example, determine whether an object implements a particular method or discover the name of its superclass.
isa变量还允许对象执行内省——以了解它们自己(或其他对象)。编译器在数据结构中记录有关类定义的信息,以供运行时系统使用。运行时系统的函数在运行时使用isa查找此信息。例如,使用运行时系统,您可以确定对象是否实现特定的方法,或者发现其超类的名称。
It’s also possible to give the compiler information about the class of an object by statically typing it in source code using the class name. Classes are particular kinds of objects, and the class name can serve as a type name. See Class Types and Enabling Static Behavior.
通过使用类名在源代码中静态地键入对象,还可以向编译器提供有关对象类的信息。类是特定类型的对象,类名可以用作类型名。参见类类型和启用静态行为。
In any program, it is important to ensure that objects are deallocated when they are no longer needed—otherwise your application’s memory footprint becomes larger than necessary. It is also important to ensure that you do not deallocate objects while they’re still being used.
在任何程序中,确保对象在不再需要时被释放都是非常重要的——否则应用程序的内存占用将超过需要。同样重要的是,确保在对象仍在使用时不释放它们。
Objective-C offers three mechanisms for memory management that allow you to meet these goals:
Objective-C提供了三种内存管理机制,可以让你实现以下目标:
Automatic Reference Counting (ARC), where the compiler reasons about the lifetimes of objects.
自动引用计数(ARC),其中编译器推断对象的生存期。
Manual Reference Counting (MRC, sometimes referred to as MRR for “manual retain/release”), where you are ultimately responsible for determining the lifetime of objects.
手工引用计数(MRC,有时也称为MRR,表示“手工保留/发布”),其中您最终负责确定对象的生存期。
Garbage collection, where you pass responsibility for determining the lifetime of objects to an automatic “collector.”
垃圾收集,将确定对象生存期的责任传递给自动“收集器”。
网友评论