How to call/access a singleton method? - ios

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.

Related

Unable to import Swift code to Objective-C project

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?

Call Swift APIs in Objective-C File

In Swift, I extend the class "Date" to add one method named "swiftDate" used to print it self. May I use the method "swiftDate" like [dateObject swiftDate] in Objective-C environment?
You need to import ProjectName-Swift.h in Bridging header file then you can access swift method in your objective c class.
Yes, ofcourse you can use it but you need to import NSDate_SwiftDate.h into bridging file ProjectName-Bridging-Header.h and also import ProjectName-Swift.h which is automatically generated when you create .h and .m file into swift project.
Yes, You can do it.
Just Import swift class and initialise instance of swift class.
and call your method like
[swiftInstance swiftInstanceMethod];
Today, I look up the Apple Developer Reference and find that the Date is a Class declared in Swift, instead of Objective-C. So, I can not only use it in Objective-C, not to mention using the extended methods in Swift.
Maybe, I should extend its ReferenceType----NSDate. The classes that conform the protocol ReferenceConvertible have a typealias ReferenceType. For Date, its ReferenceType is NSDate.
You can look up the link to learn more about ReferenceConvertible.

How to work on two UIViewControllers one in swift and other in objective-c..? [duplicate]

This question already has answers here:
How do I call Objective-C code from Swift?
(17 answers)
Closed 6 years ago.
I am rewriting my app which was in Objective-C to Swift.
I was wondering if I can use some old UIViewControllers from my previous app in my new application without having to rewrite these in Swift.
Is it possible?
Yes you can use a bridging header.
When you import Objective-C files into a swift project (let's call the project MyApp), it should ask if you want to create the bridging header MyApp-Bridging-Header.h. If it doesn't ask, you can always create it yourself, but if you do it this way make sure you include it under the Swift Compiler - Code Generator -> Objective-C Bridging Header in the Build Settings of your project. Inside this file you can write the import for your Objective-C file e.g.:
#import "MyUIViewController.h"
This will import them to the project, so that they are compatible with the other Swift files.
Then in your swift class you can refer to that Objective-C view controller like you would any other swift class, e.g.:
let myUIViewController = MyUIViewController()
Yes it will work
I also work with my previous project and i imported my old Objective-C code into the Swift project making Bridge header for accessing that Objective-C UIViewController from Swift class. You directly assign your Objective-C UiViewcontroller class in the StoryBoard UIviewController and no need to rewrite any coad again for UIViewController.if you want to access Objective-C class inside your Swift Controller Class you need to make first Bridge header file then do this.
//In Swift Controller you can call your Objective-C Class
let myView = MyController() // This is Objective-C controller
myView.updatemyProfile() // Can call function like Objective-C here

Import ViewController.swift in Objective C file

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

Using Swift classes inherited from Objective C libraries code in Objective C code

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"

Resources