Bittoo Bittoo - 16 days ago 7
Swift Question

How to work with Swift file in Objective-C?

I want to use one third party library which is written in Swift but I am working in Objective-C environment. How to do that? Until now what I have done is:


  1. Drag and drop the third party library into my project.

  2. It is asking for the bridging header I clicked on yes.

  3. In Build settings
    defines Modules
    no to
    yes
    in both.

  4. Product Module Name showing like
    $(PRODUCT_NAME:c99extidentifier)
    . I have tried with my project name also.



Now I am getting plenty of errors like below:


pDatePicker.swift:50:9: Consecutive declarations on a line must be separated by ';'


and


Ambiguous use of 'open'.


I am getting errors around 50 to 60 same as above errors. Where is my mistake? Can anybody help me?

Answer

Are you able to see projectName-Swift.h file in your project ?

If yes then, Hope you have imported #import "projectName-Swift.h" in Objectice C .m file ?

#import "projectName-Swift.h"

Then only you would be able to access objective c files in swift code.

Refer

https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

  • Check in build setting
  • Objective-C Bridging Header
  • ProjectName-Bridging-Header.h has perfect location.

if there is problem with that path than bridging will not work properly , that why when you compile #import "projectName-Swift.h" is not getting generate.

Download

Download above sample project.

enter image description here