当前在Objective-C/Swift混合项目中的导入方式 [英] Current way of importing in a mixed Objective-C / Swift project

查看:82
本文介绍了当前在Objective-C/Swift混合项目中的导入方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在追寻一些循环依赖,我试图弄清楚什么是最新的导入方式.谷歌搜索用处不大,因为在没有明确答案的情况下,这种东西会代代相传.

I've been chasing some circular dependencies recently and I'm trying to figure out what the most up to date way of importing things is. Googling isn't much use since that turns up generations upon generations of iterations for this stuff without a clear answer.

我现在在MyViewController.h文件中拥有的内容:

What I have now in the MyViewController.h file:

@class ForwardClass;
@protocol ForwardProtocol;
typedef NS_ENUM(NSUInteger, XYZCharacterType);

@interface MyViewController: UIViewController

@property (strong, nonatomic) ForwardClass *fw;

@property (nonatomic, strong) id<ForwardProtocol> fwProtocol;

@property XYZCharacterType charType;

@end

并在MyViewController.m文件中:

@import MBProgressHUD;

#import "SomeManager.h"
#import "SomeOtherViewController.h"
#import "Model.h"

#import "MyViewController.h"

#import "MyProject-Swift.h"

@implementation MyViewController

@end

因此,.h文件不再导入任何内容,而仅向前声明声明其句柄所需的文件.

So the .h file does not import anything anymore and only forward declares the files that it needs to get a handle on.

这是最新的做事方式吗?我应该知道对此有什么限制吗?

Is this the most current way of doing things? Are there any limitations to this that I should be aware of?

推荐答案

您的.h是最好,最现代的方法,您应始终避免.h文件中的任何#import.

Your .h is the best and most modern way of doing it, you should always avoid any #import in .h files.

根据我的经验,大多数人还是喜欢使用#import <Cocoa/Cocoa.h>而不是@import Cocoa;.

From my experience most people still prefer to use #import <Cocoa/Cocoa.h> over @import Cocoa;.

如果您选择使用模块#import将自动执行@import,因此无需使用它.

If you opt-in to use modules #import will do @import automatically so there is no need to use it.

还可以更清楚地了解此类中使用了哪些依赖项,并且它不会导入在当前作用域中可能不需要的任何内容.

Also its clearer which dependencies are being used in this class, and it won't import anything that you might not need in the current scope.

这篇关于当前在Objective-C/Swift混合项目中的导入方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆