1
votes

There are lots of question about how to use swift code in objective c. I had swift based project. there is Objectice C file where i need to use swift code.

Xcode did not create ProjectName-Swift.h automatically. So i created manually and check the following things.

  1. Generated interface header name : ProjectName-Swift.h

  2. Product Module Name : myproject

  3. Defines Module : YES

  4. Embedded Content Contains Swift : YES

  5. Install Objective-C Compatibility Header : YES

  6. Add @objc in swift class

import UIKit import SwiftyJSON

@objc class User: NSObject, NSCoding { }

Then Import ProjectName-Swift.h in objective c file. But gives error Unknown Type name User

I had tried with add @class User It gives error forward reference using @class

How can I fix this erros

1
There are two headers. You have confused them. You cannot manually create the generated interface header. It is not part of the project.matt
@matt can you please explain more. I am still confusedSunny Shah
@SunnyShah , Apple already explained it very easy way , please read it carefully - developer.apple.com/library/ios/documentation/Swift/Conceptual/…Anupam Mishra
@SunnyShah how did you manage to solve this problem? Where did you found that file to import swift class?Saad
You could also get this error when placing 'MyProject-Swift.h' within #ifdef FB_SONARKIT_ENABLED or #if DEBUG - then it works in simulator but not when building a release.scbojer

1 Answers

1
votes

Hopefully you have already solved the problem after reading the documentation referenced by @AnupamMishra, but just in case: try removing from the project the ProjectName-Swift.h file that you created manually. It hides the file of the same name auto-generated by Xcode and not listed in your project. The file is still there, somewhere deep in the DerivedData directory.

Another observation: you didn't have to set Objective-c Generated Interface Header Name in Build Settings. Xcode would generate one and name it myproject-Swift.h by default, since myproject is your Product Module Name.