4

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

rai212
  • 711
  • 1
  • 6
  • 20
  • 1
    That's not what it looks like, because that's invalid code. It's probably this: `@interface MyFile () { NSString * anotherPropt } @end @implementation MyFile - (id) init {...`, in which case see: http://stackoverflow.com/questions/9751057/what-is-the-interface-declaration-in-m-files-used-for-in-ios-5-projects – jscs May 18 '12 at 06:47
  • You're right, I posted it from memory and I forgot the parenthesis – rai212 May 18 '12 at 07:03

1 Answers1

5

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.

Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25
  • You're right, I forgot the parenthesis. Thank you for your explanation, I understand it now – rai212 May 18 '12 at 07:08
  • 1
    Categories can make non-public methods too (as far as any method is "non-public" in ObjC, anyways), as long as their declaration is hidden. The differences are that methods declared in extensions must be defined in the class's primary @implementation block, and that extensions can (as of a few compiler revisions ago) declare ivars. – jscs May 18 '12 at 07:09
  • @roronoa How to access interface class methods inside other class implementation? i try to declare that interface class name in other view class that class name not populated.... – SnakingPrabhu May 16 '13 at 11:28