siddle
siddle

Reputation: 137

Open view Controller of Swift from Objective C class

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.

Upvotes: 1

Views: 3890

Answers (1)

Umang Loriya
Umang Loriya

Reputation: 878

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!

Upvotes: 7

Related Questions