Objective-C 编程语言官网文档(六)-类别以及扩展

41 篇文章 0 订阅
14 篇文章 0 订阅

声明:本文档仅为个人学习过程中顺手翻译之作,方便开发的同胞借鉴参考。如有觉得译的不好不到位的地方,欢迎指正,将及时做出更正

尽量尊重原文档,因为首次Objective-C,有些地方可能直译了没有注意该语言的专有词,希望指正。如需转载,请注明出处


我的编程环境:

IDE:XCODE4.3.1

OS: MAC OS X 10.7.4

文章来译自:http://developer.apple.com/



类别以及扩展

类别允许我们为一个已经存在的类添加方法—即使你没有这个类的源码。类别允许对已经存在的类的功能进行扩展(无需继承)的特性很好很强大。使用类别,你还可以把你自己的类的实现分不到几个不同的文件中。相似的还有类扩展,但它允许额外要求的API可以声明在这里而不是在主类的 @interface 块中.

给类添加方法

通过在接口文件的类别名后声明方法并在实现中以相同名字定义它们来为一个类添加方法。类别名指明了这些方法是声明在某处的一个类的额外的方法,而不是一个新类。但是,你不能使用一个类别为一个类添加额外的实例变量。

类别添加的方法称为类的类型的一部分。例如,在一个类别中被添加到 NSArray 类中的方法被包含在了编译器所期望的 NSArray 实例应该在其指令系统中。但是,如果为 NSArray 类添加的方法是在一个子类中,就不会被包含在 NSArray 类型中. (这个只对静态赋予类型的对象有影响,因为只有静态赋予类型时,编译器才知道对象的类。)

类别方法可以做任何像定义在类中的方法可以做的事情。运行时,它们没有任何区别。为类添加的类别方法会被该类的所有子类继承,就像其它方法一样。

类别接口的声明看起来很像一个类接口的声明—除了类别名是列在类名后面的圆括号里,并且不用指明父类。如果类别方法想要访问类的任何实例变量,类别就必须导入它所扩展的类的接口文件。

#import "ClassName.h"
 
@interface ClassName ( CategoryName )
// method declarations
@end

要注意一个类别是不能为类声明实例变量的。而只能包含方法。但是,所有类作用域中的实例变量同样也在类别的作用域中。包括类声明的所有实例变量,甚至那些声明为 @private 的。

你可为一个类添加的类别的数量没有限制,但是每个类别名必须是唯一的,并且每个类别应当声明/定义不同的方法。

扩展

类扩展就像匿名的类别,除了扩展中声明的方法必须在主 @implementation 块中实现。使用 Clang/LLVM 2.0 编译器时,你还可以在一个类扩展中定义属性以及实例变量。

通常我们用类扩展来声明公有的只读属性和私有的读写属性。

@interface MyClass : NSObject
@property (retain, readonly) float value;
@end
 
// Private extension, typically hidden in the main implementation file.
@interface MyClass ()
@property (retain, readwrite) float value;
@end

要注意(与category不同) ,在第二个 @interface 块中的圆括号里没有给任何名字.

通常我们也会看到,一个类拥有公有的API,然后有一些私有的仅被该类/或者该类所在的框架使用的额外的方法。类扩展允许声明一个类额外需求的方法,方法定义在类扩展中,而不是在主类的 @interface 块中, 例如:

@interface MyClass : NSObject
- (float)value;
@end
 
 
@interface MyClass () {
    float value;
}
- (void)setValue:(float)newValue;
@end
 
@implementation MyClass
 
- (float)value {
    return value;
}
 
- (void)setValue:(float)newValue {
    value = newValue;
}
 
@end

setValue: 方法的实现必须放在类的主 @implementation 块中(不能在类别中实现),否则,编译器就会发出一个警告,说无法找到 setValue:的方法定义。



英文原文:


点击打开链接

Categories and Extensions

category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing. Using categories, you can also distribute the implementation of your own classes among several files.Class extensions are similar, but allow additional required APIs to be declared for a class in locations other than within the primary class @interface block.

Adding Methods to Classes

You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class. You cannot, however, use a category to add additional instance variables to a class.

The methods the category adds become part of the class type. For example, methods added to the NSArray class in a category are included as methods the compiler expects an NSArray instance to have in its repertoire. Methods added to the NSArray class in a subclass, however, are not included in the NSArray type. (This matters only for statically typed objects because static typing is the only way the compiler can know an object’s class.)

Category methods can do anything that methods defined in the class proper can do. At runtime, there’s no difference. The methods the category adds to the class are inherited by all the class’s subclasses, just like other methods.

The declaration of a category interface looks very much like a class interface declaration—except the category name is listed within parentheses after the class name and the superclass isn’t mentioned. Unless its methods don’t access any instance variables of the class, the category must import the interface file for the class it extends:

#import "ClassName.h"
 
@interface ClassName ( CategoryName )
// method declarations
@end

Note that a category can’t declare additional instance variables for the class; it includes only methods. However, all instance variables within the scope of the class are also within the scope of the category. That includes all instance variables declared by the class, even ones declared @private.

There’s no limit to the number of categories that you can add to a class, but each category name must be different, and each should declare and define a different set of methods.

Extensions

Class extensions are like anonymous categories, except that the methods they declare must be implemented in the main @implementation block for the corresponding class. Using the Clang/LLVM 2.0 compiler, you can also declare properties and instance variables in a class extension.

A common use for class extensions is to redeclare property that is publicly declared as read-only privately as readwrite:

@interface MyClass : NSObject
@property (retain, readonly) float value;
@end
 
// Private extension, typically hidden in the main implementation file.
@interface MyClass ()
@property (retain, readwrite) float value;
@end

Notice that (in contrast to a category) no name is given in the parentheses in the second @interface block.

It is also generally common for a class to have a publicly declared API and to then have additional methods declared privately for use solely by the class or the framework within which the class resides. Class extensions allow you to declare additional required methods for a class in locations other than within the primary class@interface block, as illustrated in the following example:

@interface MyClass : NSObject
- (float)value;
@end
 
 
@interface MyClass () {
    float value;
}
- (void)setValue:(float)newValue;
@end
 
@implementation MyClass
 
- (float)value {
    return value;
}
 
- (void)setValue:(float)newValue {
    value = newValue;
}
 
@end

The implementation of the setValue: method must appear within the main @implementation block for the class (you cannot implement it in a category). If this is not the case, the compiler emits a warning that it cannot find a method definition for setValue:.


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值