Cannot access Swift Framework in Objective-C

use swift framework in objective-c
use swift class in objective-c
how to use swift library in objective-c project
initialize swift class in objective-c
call swift viewcontroller from objective-c
swift objective-c interoperability
swift framework objective-c bridging header
use swift protocol in objective-c

Create Swift Framework ,

Geolens.swift

When i am trying to access in Objective-C project , cannot call 2 methods

createuser & startSessionForUser

Getting error like this

Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C

Your problem is in errorCode: Int? in UserCompletionBlock.

Int is value type, and Objective C cannot represent Optional of value type, only reference types such as class will.

So you have to either:

  1. get rid of optionality
  2. use reference type, such as NSNumber or your own wrapper
  3. drop this parameter completely

Cannot access Swift Framework in Objective-C, Your problem is in errorCode: Int? in UserCompletionBlock . Int is value type , and Objective C cannot represent Optional of value type , only  You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code.

Add @objc:

@objc public func createUser(...) { ... }
@objc public func startSessionForUser(...) { ... }

Can't access Swift class from Objective-C - Using Swift, My Objective-C file is importing "Module-Swift.h", and that file has some of my other Swift Can't access Swift class from Objective-C are included in the generated header, since it's part of your framework's public interface. It will be same when using the Swift file outside the framework. This(ModuleName-Swift.h) header file contains an interface for all Swift declarations. Now we can initialize the class which we created in the Swift file. Interoperability. Before we call the Swift code in an Objective-C file, keep in mind not everything is accessible.

If you want the methods to be accessible in Obj-C also please make sure you add @objc public in definition of all the methods. Just the way you did it in didRegister... and didFailedToRegisrer... methods in your class.

Happy Coding...😊

Setting up Swift and Objective-C Interoperability - iOS App , Objective-c cannot read top-level swift classes. So if you go to use a method or variable from your swift class directly it will not be recognized. There are two simple solutions to this issue. First, add @objc before the class declaration in the swift file. The contents of the Objective-C files in that framework are automatically available from any Swift file within that framework target, with no import statements. Use classes and other declarations from your Objective-C code with the same Swift syntax you use for system classes.

Link Static C Library to Swift Framework As A Private Module, Module map and header files are for frameworks. If you try to use Bridging-​Header.h in your framework Xcode will give you an error message like that: using​  Fill out an Objective-C bridging header if you need to access Objective-C code from the same app target in your Swift file. To make your Swift class accessible and usable back in Objective-C, make it a descendant of an Objective-C class.

How to use this framework with objective-c? · Issue #28 · hyperoslo , As is, it will not run from Obj-C. Obj-C cannot use Swift structs or functions outside of a class declaration. To use this as is, you need to add a  The Contacts framework provides Swift and Objective-C API to access the user’s contact information. Because most apps read contact information without making any changes, this framework is optimized for thread-safe, read-only usage.

It Looks Like You're Still Trying to Use a Framework, To prove that the framework works, we'll use an executable iOS project. In Xcode, use the File -> Add Files menu to add your framework to the project. '​Coxswain' is unavailable: cannot find Swift declaration for this class. Swift is completely compatible with Objective-C when developing apps in Cocoa Touch, the mobile framework for iOS. It works alongside Objective-C, so developers can interface between the two languages, create mixed-language apps, and take advantage of Cocoa Touch classes with Swift, and Swift classes with Objective-C.

Comments
  • Please, do not include code as screenshots. Paste code as text instead.