目标C,由于循环依赖而导致的Swift互操作性问题 [英] Objective C, Swift Interoperability issue due to circular dependency

查看:108
本文介绍了目标C,由于循环依赖而导致的Swift互操作性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个iOS项目中进行工作,该项目是在几年前在Objective C中完成的。因此,我必须为现有功能实现一些新功能,所以这次我使用Swift来实现这一目的。

I'm working on an iOS project that was done few years back in Objective C. So I've to implement some new features to the existing one, so this time I'm using Swift for that purpose.

我添加了一个新的Swift类:

I've added a new Swift class:

class CampView: UIView
{
   // Code
}

我要访问此文件我现有的Objective C类之一。所以我确实喜欢:

I want to access this class in one of my existing Objective C class. So I did like:

@class CampView;
@interface NewCampViewController : UIViewController

@property (nonatomic, strong) IBOutletCollection(CampView) NSArray *campTypes;

@end

但是当我将其连接到Storyboard时,它是崩溃并显示一条消息:

But when I connect it to my Storyboard, it is crashing with a message:


由于未捕获的异常 NSUnknownKeyException而终止应用程序,
原因: [setValue:forUndefinedKey:] :
此类不适用于密钥campTypes的键值编码。'

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key campTypes.'

我也无法导入我的系统生成的Swift头文件到这个特定的类,当我这样做时,它会引发如下错误:

Also I can't import my system generated Swift header file to this particular class, when I do that it throws an error like :


'MyApp-Swift.h'文件找不到

'MyApp-Swift.h' file not found

这就是为什么我在上面的片段中使用 @class CampView; 。在调查过程中,我发现我的 NewCampViewController.h 包含在Objective C桥接标头中。因此怀疑它是由于循环依赖所致,但是我还不能解决。

That's why I used @class CampView; in the above snippet. While investigating I found that my NewCampViewController.h is included in the Objective C bridging header. So suspect it is due to circular dependency, but I couldn't fix it yet.

有人可以帮助我解决此问题吗?

Can anybody please help me to fix this issue ?

推荐答案

1)您是否正在执行导入(导入 MyApp-Swift.h )从您的Objective-C类的.m文件中?这应该避免循环依赖。如果您正在执行@class CampView,则Xcode希望您将Camp.View类导入.m文件中。 @class基本上只是头文件的转换。

1) Are you doing the import (import "MyApp-Swift.h") from the .m file of your Objective-C class? This should avoid a circular dependancy. If you are doing @class CampView, Xcode expects you will import CampView class in the .m file. @class is basically just a cast for your header file.

如果在执行-swift.h导入时遇到找不到文件的错误,请尝试通过查看此内容进行调试​​帖子。另外,我相信如果您更改项目的名称,导入名称可能不会随之更改。检查模块的名称。

If you are getting the file not found error doing the -swift.h import, trying debugging by looking through this post. Also, I believe if you change the name of your project, the import name may not change along with it. Check the name of your module.

如果您仍然认为通过循环导入可能会找不到此文件,则最简单的验证方法是添加导入到另一个您不会循环调用的obj-c文件,并初始化CampView的测试实例。

If you still believe it is possible that you are getting this file not found issue from a circular import, the easiest way to verify that is the case would be to add the import to another obj-c file you know will not have a circular call, and init a test instance of CampView.

2)问题是如@ Paulw11所建议的那样,在IB中,您可以通过检查代码中连接的属性旁边的圆点来检查是否已建立连接。单击它应显示它已连接到什么。空点未连接。

有关如何从IB正确建立连接的更多信息到代码中的媒体资源,请参见此链接

2) If the problem is as @ Paulw11 suggests, the connection in IB, you can check if the connection is made by checking checking the round dot next to the property connected in code. Clicking on it should show what it is connected to. An empty dot is not connected. For more information on how to properly make a connection from IB to a property in your code see this link.

这篇关于目标C,由于循环依赖而导致的Swift互操作性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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