Declare interface inside implementation file (

2020-06-23 07:10发布

问题:

In the last verson ox xCode (4.3) I've seen that prefdefined templates (such us Master/Detail template) in which the interface declaration is made in the .m file. For example, in the file MyFile.h there is:

@interface MyFile

@property (nonatomic, retain) NSString *someProp;

@end

And in the MyFile.m file there is:

@implementation MyFile

@interface MyFile {
    NSString * anotherProp;
}

- (id) init...

Why it's made on this way? Why the anotherProp isn't declared into the MyFile.h file?

Thanks in advance

回答1:

Well its not declared this way but this way :-

@interface ClassName() {

    Declarations;

}

Methods;

@end

These are called class extension.They are similar to categories but can be declared only in implementation of the class not in any other class.The use of extensions is to redeclare property that is public or readwrite , also declare newer ones , if needed.They simply allow you to declare properties and variables in places other than @interface so the name extensios.

It was inrtoduced to tackle the problem with categories as they make the methods public and data hiding capability of classes is compensated but a class extension effectively extends the class’s primary interface which the declared methods have the same requirements as methods declared in the class’s oft public primary interface.