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

2
  • 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: stackoverflow.com/questions/9751057/… Commented May 18, 2012 at 6:47
  • You're right, I posted it from memory and I forgot the parenthesis Commented May 18, 2012 at 7:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You're right, I forgot the parenthesis. Thank you for your explanation, I understand it now
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.
@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....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.