Class Names in Source Code
在代码中,类名只能出现在两个不同的上下文中,这两个上下文反应了类名可以作为数据类型也对象(类对象)的角色.
● The class name can be used as a type name for a kind of object. For example:
Rectangle *anObject; ///1.作为数据类型.标志某种对象.
Here anObject is statically typed to be a pointer to a Rectangle object. The compiler expects it to have the data structure of a Rectangle instance and to have the instance methods defined and inherited by the Rectangle class. Static typing enables the compiler to do better type checking and makes source code more self-documenting.
Only instances can be statically typed; class objects can’t be, because they aren’t members of a class, but rather belong to the Class data type.
//只有实例对象才能使用静态类型确定,类对象是一种 Class 的数据类型,不能使用静态类型确定
● As the receiver in a message expression, the class name refers to the class object. The class name can stand for the class object only as a message receiver.
In any other context, you must ask the class object to reveal its id (by sending it a class message). This不当example passes the Rectangle class as a parameter in an isKindOfClass: message:
if ( [anObject isKindOfClass:[Rectangle class]] ) //2.作为消息接收者
...
It would have been illegal to simply use the name “Rectangle” as the parameter. The class name can only be a receiver.
If you don’t know the class name at compile time but have it as a string at runtime, you can use NSClassFromString to return the class object:
NSString *className;
...
if ( [anObject isKindOfClass:NSClassFromString(className)] ) //如果类名是字符,,,,,
...
This function returns nil if the string it’s passed is not a valid class name.
Class names exist in the same namespace as global variables and function names. A class and a global variable can’t have the same name. Class names are the only names with global visibility in Objective-C.
全局变量不能与类名相同,在Object C 中类名是唯一具有全局可见性的.
Testing Class Equality
得到正确的类是非常重要的,这可以通过指针比较两个类对象是否相同,在Cocoa framework 有些特性可以动态地透明地子类化现有的类,以扩展他们的功能,如:ey-value observing and Core Data
You can test two class objects for equality using a direct pointer comparison. It is important, though, to get the correct class. There are several features in the Cocoa frameworks that dynamically and transparently subclass existing classes to extend their
functionality (for example, key-value observing and Core Data do this—see
Key-Value Observing Programming Guide and Core Data Programming Guide respectively).
在动态扩展子类通常覆盖原来的类的方法,或者说假扮成原来的类的方法,当做类相等测试时候,就要通过测试class 方法的返回值,
In a dynamically-created subclass, the class method is typically overridden such that the subclass masquerades (假扮;乔装) as the class it replaces. When testing for class equality, you should therefore compare the values returned
by the class method rather than those returned by lower-level functions. Put in terms of API, the following
inequalities pertain for dynamic subclasses:
[object class] != object_getClass(object) != *((Class*)object)
You should therefore test two classes for equality as follows:
if ([objectA class] == [objectB class]) { //...需要这样测试类是否相同