I have a view controller with objective c class , now i want to open the view controller that contain class swift. I have tried several times but it is not opening th swift class how can i open the swift class from objective c class.
-
you should use a bridging header. Please search to find more information about it.D. Greg– D. Greg2017-11-09 06:22:41 +00:00Commented Nov 9, 2017 at 6:22
-
i have bridging header i'm not getting now to call the swift class from objective c. @D.Gregsiddle– siddle2017-11-09 06:27:45 +00:00Commented Nov 9, 2017 at 6:27
-
Are you using storyboard or XIB? This may help you pinkstone.co.uk/how-to-use-swift-classes-in-objective-cNagarjun– Nagarjun2017-11-09 09:09:23 +00:00Commented Nov 9, 2017 at 9:09
-
I'm using storyboard .@Nagarjunsiddle– siddle2017-11-09 09:16:19 +00:00Commented Nov 9, 2017 at 9:16
-
Bro i have followed the link u have given but i'm getting the file not found error , I have also changed the header name as said in a tutorials. @Nagarjunsiddle– siddle2017-11-09 09:26:55 +00:00Commented Nov 9, 2017 at 9:26
1 Answer
I initially faced same trouble but after searching on web I got success. you should follow exact same as I have mentioned below:
Step by step Swift integration for Xcode Objc-based project:
Create new *.swift file (in Xcode) or add it by using Finder
Create an Objective-C bridging header when Xcode ask you about that
Implement your Swift class with @objc attribute:
import UIKit
@objc public class CustomView: UIView {
override func draw(_ rect: CGRect) {
// Drawing code
}
}
Open Build Settings and check those parameters:
Defines Module : YES
Product Module Name : myproject
Make sure that your Product Module Name doesn't contain any special characters
- Install Objective-C Compatibility Header : YES
Once you've added *.swift file to the project this property will appear in Build Settings
- Objective-C Generated Interface Header : myproject-Swift.h
This header is auto-generated by Xcode
- Objective-C Bridging Header : $(SRCROOT)/myproject-Bridging-Header.h
Import Swift interface header in your *.m file
#import "myproject-Swift.h"
Don't pay attention to errors and warnings. Clean and rebuild your Xcode project. Done!