views:

20

answers:

3

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.

+1  A: 

Yes you can do that.

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

jtbandes
OK, thanks, so I'm guessing I should just use #import then? From Apple: "This directive is identical to #include, except that it makes sure that the same file is never included more than once. It’s therefore preferred and is used in place of #include in code examples throughout Objective-C–based documentation." Also, how would I add multiple classes in the one main.m objective-c file? Would I specify just more than one in the @interface and @implementation sections? I'm guessing that's the case versus restating those sections..
BOSS
jtbandes
+1  A: 

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.

Chuck
+1  A: 

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.

Kiran