开发者

Useless #import in apple.dev tutorial

开发者 https://www.devze.com 2023-01-14 23:21 出处:网络
I\'m reading the Core Data Utility tutorial from apple documentation and there\'s a part that\'s bugging me a bit.

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&g开发者_运维问答t;
#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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消