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
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 am working on a project which contains Objective C, Objective C++, and Swift4 classes. I am trying to access the .mm file (i.e Objective C++ file) into swift class but it's giving me an error and showing it's undeclared. I have added a bridging header to the project and imported Objective C++ file's header there.
Example to understand the problem:
I have A.mm class which has B.mm class imported in it. after adding A class into the bridging header compiler is giving an error in B class.
Error In B.h :
Please help
Swift can only bridge to C and Objective-C. If your missing class uses C++ types in its header, Swift will be unable to read that file and just skip that class.
Can you change your Objective-C++ class so it only uses C++ types in its .mm file, and not in its .h file? Then it will look like a regular Objective-C class to Swift and you should be able to use it.
Alternately, you may be able to create a second class that is ObjC „on the outside“ and calls the ObjC++ class in its .mm, and that just hands through all the calls, translating C++ data types into simple C types or ObjC objects.
Look into „class extensions“ for declaring C++ properties and instance variables that would usually go into your header in the .mm file instead.
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.
Here is the code that I want to call in Swift:
+ (Client*) clientWithInfo:(ServerInfo*)info {
return [[[Client alloc] initWithInfo:info] autorelease];
}
Here is how I am calling it in Swift:
Client.clientWithInfo(ServerInfo)
But it gives me the following error:
clientWithInfo unavailable: use object construction Client(info:)
First you need to have bridging headers and in this file included name of your class.
#import "MySingletonClass.h"
After you should be able to call MySingleton.sharedInstance.methodName
Start by reading Apple's guide Using Swift with Objective-C (Swift 2.1). There's a section on importing Objective-C into Swift.
You need to create a Objective-C bridging header file. When you add a Swift file to an Objective-C project or an Objective-C file to a Swift project Xcode will ask you if you want to add an Objective-C bridging header?
To use your Objective-C code with your Swift code you would import every Objective-C header you want to expose to swift. Note that this is for the same target.
#import "MyCustomObjectiveCCodeWithSingleton.h"
From the documentation:
Any public Objective-C headers listed in this bridging header file
will be visible to Swift. The Objective-C functionality will be
available in any Swift file within that target automatically, without
any import statements. Use your custom Objective-C code with the same
Swift syntax you use with system classes.
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"