I have a swift project and my initial UIViewController
is a swift class.
From the initial ViewController
, I added a new Viewcontroller
in my storyboard
and set an objective-c class (FirstViewController.h
) for it.
In my project, I have another swift class (DataManager.Swift
) which is a subclass of NSObject
.
Now I want to create an instance of this DataManager.swift
in my objective-C class (FirstViewController.m
). But unfortunately, i couldn't create this instance, giving me an error like -
Receiver '
DataManager' for class message is a forward declaration
What can I do now to resolve this issue? Is it possible to access swift class from Objectibe-C class while project is in swift?
My FirstViewController.m,
#import "FirstViewController.h"
@class DataManager;
@interface FirstViewController ()
@property (nonatomic, strong) DataManager *dataManager;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_dataManager = [[DataManager alloc] init]; //This line giving me that error
}
@end
My DataManager.swift class,
import UIKit;
@objc class DataManager: NSObject {
var a: Int = 0
}
My BridgingHeaderFile,
#ifndef BridgingHeader_h
#define BridgingHeader_h
#import "FirstViewController.h"
#endif
You should import your swift module into your .m file with this syntax:
#import "ModuleName-Swift.h"
where ModuleName
usually is your Project Name.
NOTE: adding -Swift
suffix is important!
Also @class DataManager;
will be redundant after import.