Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the logic of putting @interface in .m file? [duplicate]

Tags:

objective-c

Possible Duplicate:
Difference between @interface definition in .h and .m file
What is the @interface declaration in .m files used for in iOS 5 projects?

I've seen code like this:

// In Header.h
@interface Header{}
@end

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

My questions are:

  1. What's the difference in putting it in 2 files?
  2. Why put {} after class name in ".h" file and why "()" in ".m" file?
like image 856
itsaboutcode Avatar asked Mar 22 '12 17:03

itsaboutcode


2 Answers

@interface MyClass(){
    NSInteger aInt;

}
@property(nonatomic,strong) NSString *name;
@end

is a class extension

with modern compilers this is a great way, to decrale methods, ivars and properties only for private use in the class MyClass.

Class extensions have to be declared in the main implementation file (not in a category).

So you can hide implementation details from the header file, they are private.

like image 114
vikingosegundo Avatar answered Nov 05 '22 23:11

vikingosegundo


This has become a common practice for declaring "private" properties/methods for a given class. By declaring them in an anonymous class extension inside the .m, these properties/methods are not exposed to consuming objects.

like image 23
Ian L Avatar answered Nov 05 '22 22:11

Ian L