1

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.

6
  • you should use a bridging header. Please search to find more information about it. Commented Nov 9, 2017 at 6:22
  • i have bridging header i'm not getting now to call the swift class from objective c. @D.Greg Commented 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-c Commented Nov 9, 2017 at 9:09
  • I'm using storyboard .@Nagarjun Commented 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. @Nagarjun Commented Nov 9, 2017 at 9:26

1 Answer 1

7

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!

Sign up to request clarification or add additional context in comments.

8 Comments

Bro i have done YES and Product module name also but when i write header file it shows me the bridge header file only. @Umang Loriya
It shows only this #import "PK.House-Bridging-Header.h" in objective c class where i want to write header file.
Actually "PK.House-Bridging-Header.h" is for using Objective C files in Swift but what you need to do is simply add this to .m file where you need to use swift object #import "PK.House-Swift.h".
Bro i'm really sorry but i'm not getting the point. I have done the above part u said to do YES and change the name also, after doing this now what to do ? @Umang Loriya
let's assume that you have objective c class files named viewController so in viewController.m file you need to add this line to access swift class files :#import "PK.House-Swift.h"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.