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.
Generated interface header name : ProjectName-Swift.h
Product Module Name : myproject
Defines Module : YES
Embedded Content Contains Swift : YES
Install Objective-C Compatibility Header : YES
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
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.
Related
I am trying to import a swift class to my objective-c project but I get "Use of undeclared identifier" error inside my .m file when I try to use the swift class.
Inside my swift class there is an "#objc" marker. When I created the swift class I added the bridge file. I also checked those values:
Defines Module : YES
Always Embed Swift Standard Libraries : YES
Install Objective-C Compatibility Header : YES
Also in Target/Build Settings/Objective-c Bridging Header I checked that the path to the bridging file is correct.
What am I doing wrong? Do I have to write something inside the bridging file?
I have a subclass of NSOperation and custom init method which can take couple of arguments. this class is written in OBJ C. and I wanted to call this init method from Swift class for unit testing. is there a way I can call the init method directly from swift? Compiler couldn't recognize the init method.
You have to create a bridging header and import the ObjC class there. Then that class will be available in all of your swift classes
Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
Name your file “YourProjectName-Bridging-Header.h”. ...
Create the file.
Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. You may find it faster to type in “Swift Compiler” into the search box to narrow down the results. Note: If you don’t have a “Swift Compiler – Code Generation” section, this means you probably don’t have any Swift classes added to your project yet. Add a Swift file, then try again.
Next to “Objective-C Bridging Header” you will need to add the name/path of your header file. If your file resides in your project’s root folder simply put the name of the header file there. Examples: “ProjectName/ProjectName-Bridging-Header.h” or simply “ProjectName-Bridging-Header.h”.
Open up your newly created bridging header and import your Objective-C classes using #import statements. Any class listed in this file will be able to be accessed from your swift classes.
Above info was found here: http://www.learnswiftonline.com/getting-started/adding-swift-bridging-header/
I'm trying to use my new ViewController.swift file in my existing objective C project.
Below is swift file code
import UIKit
class TutorialViewController: UIViewController{
}
Below is Objective C code
#import "TutorialViewController-Swift.h" //"TutorialViewController-Swift.h" file not found
I'm unable to import swift code. I had followed all the steps in this
Please let me know, where am I making mistake. Is it only applicable for NSObject class.
Thanks in advance
You can't import a swift class directly to Objective C class like that way. By default Xcode generates a swift bridging header for this purpose. You need to import that header. Normally that header file uses the following naming convention:
<#your module name #>-Swift.h
Or you can get the value from your target's build settings:
Choose your target
Go to Build Settings tab
Go to Swift Compiler - Code Generation category
Check the value of Objective-C Generated Interface Header Name
Import that header in your objective-c class to use all your swift classes
As per document, When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h".
By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header. Declarations marked with the private modifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with #IBAction, #IBOutlet, or #objc as well. If your app target is compiled with testing enabled, a unit test target can access any declaration with the internal modifier as if they were declared with the public modifier by prepending #testable to the product module import statement.
You don’t need to do anything special to create the generated header file—just import it to use its contents in your Objective-C code. Note that the Swift interfaces in the generated header include references to all of the Objective-C types used in them. If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types before importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.
For more details follow this Importing Swift into Objective-C
I am having an issue using a custom pure Swift framework in another project.
Some notes (as I've believe I've thorougly searched for all possible answers) :
I have my classes declared as public in the framework.
I have successfully built and ran the framework with a Swift target application but only from within the framework project.
I have included my framework in Linked Framework and Libraries and Embedded Binaries.
What i am trying to do is build a pure Swift single view application project by importing only the product framework. The error I am getting is "MyClass is unavailable: cannot find Swift declaration for this class" (which as mentioned above is public) . Also I have an public enumaration with a similar error: "Use of undeclared type 'MY_ENUM'"
Example code below:
import Foundation
import UIKit
import MyFramework /// don't know if this is needed.
public class ViewController: UIViewController{
var myclass:MyClass? ///here is the above error ,it's initialized in the viewDidLoad() function
var myEnum:MY_ENUM = MY_ENUM.MY_ENUM_VALUE /// 2nd enumeration error.
override public func viewDidLoad(){
super.viewDidLoad()
self.myClass = MyClass(arguments) ///same error as above
self.myClass?.myFunction /// ViewController does not have a member 'MyClass' error
/// more code here with errors regarding the class and enum.
}
I've also tried using an objective-C Bridging Header (though I believe this is wrong) and importing my framework header.
#import <MyFramework/MyFramework.h>
Is it possible for a solution to the above or is a restriction from Swift and I am trying something in vain?
One final note: I've included some other headers from another Objective-C framework in my framework because it was the only way to build it as a custom Swift framework. The classes there are visible to the Swift application.
P.S.If more code is needed I'll be happy to provide.
Solved the issue.
Had to put the .swift files together with the .h files as public headers in my custom framework (don't understand why though) and build again.
Maybe the path of your framework is wrong?
I'm working on a Swift project, using couple of ObjC libraries.
One of them is SWTableViewCell. My app's lists' cells inherit from SWTableViewCell, a subclass of UITableViewCell that adds swiping action on cells.
Libraries are added with cocoapods.
I want to import some Swift code into ObjC, within my main project. The project-Swift.h is being generated as it should (every Swift class inheriting from NSObject, annotated as #objc is being included), but it contains errors:
project-Swift.h:135:31: Cannot find interface declaration for 'SWTableViewCell', superclass of 'MySWTableViewCell'; did you mean 'UITableViewCell'?
How to remedy this situation?
I need the header to be generated properly in order to use Swift classes in ObjC
My goal is either to ignore these classes, or let XCode that it needs to import additional header during project-Swift.h
This should solve your problem:
Create a bridging header file in you project: File > New > File > (iOS or OS X) > Source > Header File
In your Objective-C bridging header file, import the needed Objective-C headers you want to expose to Swift: in your case you need #import "SWTableViewCell.h"