Sunny Shah
Sunny Shah

Reputation: 13020

Use swift code in objective C file in swift based project

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

Upvotes: 1

Views: 837

Answers (1)

Anatoli P
Anatoli P

Reputation: 4891

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.

Upvotes: 1

Related Questions