在.m中的@interface (原創)


作者: wink
(轉載請注明出處,謝謝)
//in Header.h
@interface Header{}
@end

//in Header.m
@interface Header()
@end

這是個非常常見的設計,為什么在.m文件里面,也要出現一個接口聲明呢?

這很類似於一個分類,但其實它不是一個分類(或者你也可以叫它anonymous Catogary),其實這是一個叫做class extension的東西

 

說說區別:

  1.  首先 extension 可以重聲明一個數據成員,比如一個數據成員是只讀的,你可以把它變成可讀寫

  2.  分類根本不同意你擴展數據成員,它只擴展一些方法, 但是在Clang/LLVM 2.0 compiler 以后, extension可以這么做.

  3.  有時候,你如果希望實現一些不公開的方法供自己使用,那你可以把它放入.m的extension里面.那么這些接口都是不公開的了

 

為了更清晰.下面附上官方文檔

Categories and Extensions

A 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:.




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM