运行时系统
Object c 编译与连接阶段推迟很多的决定,只要可能,象动态创建对象,决定调用哪个方法,都在运行时决定, 因此运行时系统很重要,是让编译出来的object c 代码运行的关键,
To understand
more about the functionality it offers, though, see Objective-C Runtime Programming Guide .
对象:
An object associates data (instance variables )with the particular operations (Methods) 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.
In essence, an object bundles a data structure (instance variables) and a group of procedures (methods) into a self-contained programming unit.
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.
实例变量属于对象内部所用, 如若访问对象的状态信息,只能通过他提供的方法,我们也可以Scope directives 指定起子类或其他对象能直接访问起实例变量.
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.
id 是一数据类型,用于区分对象.定义对象.一种对象的通用类型.不管什么对象,如果不知道其具体的类型,都可以通过id 来声明
Ie:
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.)
Object c 默认返回类型是id, 标准C则是int
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.
空类型 nil , id 值为0
#define nil (id)0 /* id of Nil instance */
#define Nil (Class)0 /* id of Nil class */
objc/objc.h 的在学习环境的path:C:\GNUstep\GNUstep\System\Library\Headers\objc
id is defined as pointer to an object data structure: (id 其实就是一个指向描述一个对象结构的structure 的指针)
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:
typedef struct objc_class *Class;
the isa variable is frequently referred to as the “isa pointer.”
每一个对象都会有一个isa的成员变量,其实就是指向结构objc_class 的指针
我参考的这文章是最新2.0的,但在GNU path中找到的定义则有所出入.
Dynamic Typing
The id type is completely nonrestrictive. By itself, it yields no information about an object, except that it is an
object.
id不能给出更多一个对象的信息,但下面有说,Object c 的Dynacick Typed ,
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.
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,
这段话却说isa 能提供Dynamic typed,introspection,,但isa 是id的instance varible,,好象有点矛盾.
但先不关这些矛盾,,我们先意识到,这些Object c 的特性依靠isa 让runtime system 实现就是了
Memory Managemen
memory footprint (空间)
Object C 提供3钟方式管理memory fottprint.
Automatic Reference Counting (ARC), where the compiler reasons about the lifetimes of objects.
自动引用计数
● Manual Reference Counting (MRC, sometimes referred to as MRR for “manual retain/release”), where you
are ultimately responsible for determining the lifetime of objects.
Manual reference counting is described in Advanced Memory Management Programming Guide .
● Garbage collection, where you pass responsibility for determining the lifetime of objects to an automatic
“collector.” (Ios 不支持这方式)
Garbage collection is described in Garbage Collection Programming Guide . (Not available for iOS—you
cannot access this document through the iOS Dev Center.)
Object Messaging
Message Syntax
In Objective-C,调用对象method through message.
[receiver message]
The receiver is an object, and the message tells it what to do. In source code, the message is simply the name of a method and any parameters that are passed to it. When a message is sent, the runtime system selects the appropriate method from the receiver’s
repertoire and invokes it.
ie:
NSAutoreleasePool * pool=[[NSAutoreleasePool alloc] init]; //注意有;号
[pool release];
The message is followed by a “;” as is normal for any statement in C.
Because the method name in a message serves to “select” a method implementation, method names in
messages are often referred to as selectors. (方法名称,在消息中,也叫选择子)
Methods can also take parameters, sometimes called arguments . A message with a single parameter affixes a
colon (:) to the name and puts the parameter right after the colon:
[myRectangle setWidth:20.0]; //带参数的调用
For methods with multiple parameters, Objective-C's method names are interleaved with the parameters such
that the method’s name naturally describes the parameters expected by the method. The imaginary message
below tells the myRectangle object to set its origin to the coordinates (30.0, 50.0):
[myRectangle setOriginX: 30.0 y: 50.0]; // This is a good example of
// multiple parameters
A selector name includes all the parts of the name, including the colons, so the selector in the preceding example is named setOriginX:y:. It has two colons, because it takes two parameters. The selector name does not, however, include anything else, such
as return type or parameter types.
setOrigin 是方法名称,X,y 是parameter,选择子是:setOriginX:y: 包括冒号的.
the terms “named parameters” and “keyword parameters” carry the implications that
the parameters can vary at runtime, can have default values, can be in a different order, and can possibly
have additional named parameters. None of these characteristics about parameters are true for Objective-C.
Object c 不支持命名参数,参数带默认值等其他语言的特性.
In principle, a Rectangle class could instead implement a setOrigin:: method with no label for the second
parameter, which would be invoked as follows:
[myRectangle setOrigin:30.0 :50.0]; // This is a bad example of multiple parameters ,可以不带lable,但不推荐
Note that a variable and a method can have the same name. 变量名称可以跟方法名重名
BOOL isFilled;
isFilled = [myRectangle isFilled];
Methods that take a variable number of parameters are also possible, though they’re somewhat rare. Extra
parameters are separated by commas after the end of the method name. (Unlike colons, the commas are not
considered part of the name.) In the following example, the imaginary makeGroup: method is passed one
required parameter (group) and three parameters that are optional:
[receiver makeGroup:group, memberOne, memberTwo, memberThree]; //group是必须的.其他,分割的是可选的.
Object c 也可以有可选参数,可选参数一豆号分开.
One message expression can be nested inside another. Here, the color of one rectangle is set to the color of another:
[myRectangle setPrimaryColor:[otherRect primaryColor]]; //嵌套消息的语法
Objective-C also provides a dot (.) operator that offers a compact and convenient syntax for invoking an
object’s accessor methods. 提供.操作符调用方法
Sending Messages to nil
In Objective-C, it is valid to send a message to nil—it simply has no effect at runtime. There are several patterns in Cocoa that take advantage of this fact. The value returned from a message to nil may also be valid:
以下case send message to nil 才是validate,其他case 是undefine 的
● If the method returns an object, then a message sent to nil returns 0 (nil).
For example:
Person *motherInLaw = [[aPerson spouse] mother];
If the spouse object here is nil, then mother is sent to nil and the method returns nil.
mother 这个方法必须返回一个Object,这样mother sent to nil return nil.
● If the method returns any pointer type, any integer scalar of size less than or equal to sizeof(void*),
a float, a double, a long double, or a long long, then a message sent to nil returns 0.
方法返回指针类型,或者整型大小的值,则发送给nil,return 0.
● If the method returns a struct, as defined by the Mac OS X ABI Function Call Guide to be returned in
registers, then a message sent to nil returns 0.0
for every field in the struct. Other struct data types will not be filled with zeros.
● If the method returns anything other than the aforementioned value types, the return value of a message
sent to nil is undefined.
The following code fragment illustrates a valid use of sending a message to nil.
id anObjectMaybeNil = nil;
// this is valid
if ([anObjectMaybeNil methodThatReturnsADouble] == 0.0)
{
// implementation continues...
}