简体   繁体   中英

Objective C, Swift Interoperability issue due to circular dependency

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.

I've added a new Swift class:

class CampView: UIView
{
   // Code
}

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

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

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

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' file not found

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) 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.

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.

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) 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM