Bakaburg
Bakaburg

Reputation: 3311

Useless #import in apple.dev tutorial

I'm reading the Core Data Utility tutorial from apple documentation and there's a part that's bugging me a bit.

At the beginning of "main" you got:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#include <objc/objc-auto.h>

Why you would need to include Foundation.h if it is already included in CoreData.h? the same for objc-auto.h that is inside Foundation.h? And why in the last line is used #include?

Thank you

Upvotes: 0

Views: 115

Answers (2)

mipadi
mipadi

Reputation: 410592

Why you would need to include Foundation.h if it is already included in CoreData.h?

You don't need to, but it doesn't hurt to be explicit in your code, either. #import prevents it from being imported twice, anyway. Apple's tutorial likely just wanted to emphasize that you were using some Foundation functionality.

the same for objc-auto.h that is inside Foundation.h?

I don't think objc-auto.h is included in Foundation.h (or anything included by Foundation.h); since you're using a GC function, you will have to include this header.

And why in the last line is used #include?

It's a C idiom. Apple's Objective-C style dictates that Objective-C headers are imported, C headers are included. objc-auto.h is C code. You could import it if you wanted to.

Upvotes: 0

Brian Gianforcaro
Brian Gianforcaro

Reputation: 27180

In any C based language it is good practice to include what you use. In the future for what ever reason the headers might be changed and CoreData.h may not include Foundation.h anymore.

If you don't include what you use then your progam won't compile and may become a portability nightmare across multiple versions of the API.

#imports // are for Objective-C headers

#include // are for just C headers. 

They are most likely just trying to teach good practices in anyone reading the tutorial.

Upvotes: 1

Related Questions