Qcom
Qcom

Reputation: 19213

Ability to specify more than one class declarations and definitions in objective-c interface and implementation sections respectively

I was just wondering if I have the option to specify more than one set of class declarations and definitions in the same file, without breaking it up into multiple files.

I'm guessing this is just the sign to break it up, but I was just wondering out of curiosity.

Also, bonus, when including, what is the difference between #include and #import.

Upvotes: 0

Views: 193

Answers (3)

Kiran Balegar
Kiran Balegar

Reputation: 926

Yes you can have multiple classes in a same file but i don't prefer it. Its a good habit/good design to have classes in different files. Helps a lot in re-usability.

#import ensures that a file is only ever included once so that you never have a problem with recursive includes. I think performance may go down if use #include.

Upvotes: 1

Chuck
Chuck

Reputation: 237110

Yes. The only really vital division is that a file should only be imported or compiled, not both — that is, unless all the code you feed to the compiler is in main.m, you need to have at least one header and one implementation file. The header can contain all the interface details for everything in your program and the implementation file can contain all the implementation details and it will work just like if you had separate files. You can just stack the contents of the would-be files end-to-end. That's actually what the #import and #include directives do — they literally copy the contents of the included file into the place where the directive is written.

Of course, what we're talking about here isn't a good design for a program at all.

Upvotes: 1

jtbandes
jtbandes

Reputation: 118781

Yes you can do that.

#import has built-in checks to prevent including the same file multiple times (avoiding stuff like #ifndef __MYHEADER_H...)

Upvotes: 1

Related Questions