How to Access Swift Class from Kotlin in Multiplatform?

Accessing a Swift class from Kotlin can often be a challenge, particularly in a Kotlin Multiplatform project where seamless integration is required. In this article, we will explore how to properly set up your project to ensure that your Swift class is accessible from Kotlin, diving into the configuration details that might be causing visibility issues. Understanding the Issues You mentioned you have created a Wallet Swift class, along with a bridging header. However, it seems the Kotlin side is unable to access this class. This visibility problem often arises from incorrect configuration in the bridging header or missing annotations in Swift. Swift Class Setup Let's start with your original Swift class: @objc public class Wallet: NSObject { @objc public func getPrivateKey() -> String { return "" } } This class correctly uses the @objc and public attributes to expose your Swift code to Objective-C and therefore Kotlin. Bridging Header Configuration You created a bridging header that includes the Swift class: #ifndef iosApp_Bridging_Header_h #define iosApp_Bridging_Header_h #import "iosApp-Swift.h" #endif /* iosApp_Bridging_Header_h */ This header seems correct, as it correctly imports the generated Objective-C header that includes your Swift classes. However, ensure that this bridging header is indeed set in your project's build settings under "Objective-C Bridging Header". If you're leveraging this header, the headers defined should also be visible to Kotlin. Validate that the path is accurate relative to your project structure. Usage in Kotlin Multiplatform You’ve defined your Kotlin expect class in the common module correctly as: expect class WalletMP() { fun getPrivateKey(): String } And your actual class in the iosMain module is defined as: actual class WalletMP actual constructor() { actual fun getPrivateKey(): String { return "" } } However, to call your Swift class, ensure that you instantiate Wallet correctly in your Kotlin: val wallet = Wallet() Addressing Visibility Issues If Wallet is still not accessible, consider the following steps: Check Framework Settings: Make sure that your Swift classes are included in the correct framework and that the framework is properly linked in your Kotlin project. Validate Build Settings: Look for any settings related to Swift version compatibility in both iOS and Kotlin Multi-platform. Sometimes, mismatched versions can lead to compatibility issues. Ensure Objective-C Interoperability: Verify that the Wallet class, along with methods you need, are marked with both @objc and public. This ensures Kotlin can indeed see and utilize it. Clean and Rebuild: After each configuration change, clean your build folder and rebuild your project to ensure all changes are correctly applied. Frequently Asked Questions Q: Why is my Swift class not visible to Kotlin? A: This is usually due to incorrect bridging headers, visibility attribute misconfigurations, or improper linking of your framework. Q: How can I debug visibility issues between Swift and Kotlin? A: Check your bridging header configuration, ensure proper annotations are applied, and verify all settings in your project's build configurations. Conclusion Successfully integrating Swift with Kotlin in a Multiplatform project may involve several configuration details. By ensuring your class is correctly set up with public access, defining proper bridging headers, and following the steps outlined, you're likely to resolve visibility issues. Happy coding!

May 13, 2025 - 03:06
 0
How to Access Swift Class from Kotlin in Multiplatform?

Accessing a Swift class from Kotlin can often be a challenge, particularly in a Kotlin Multiplatform project where seamless integration is required. In this article, we will explore how to properly set up your project to ensure that your Swift class is accessible from Kotlin, diving into the configuration details that might be causing visibility issues.

Understanding the Issues

You mentioned you have created a Wallet Swift class, along with a bridging header. However, it seems the Kotlin side is unable to access this class. This visibility problem often arises from incorrect configuration in the bridging header or missing annotations in Swift.

Swift Class Setup

Let's start with your original Swift class:

@objc public class Wallet: NSObject {  
    @objc public func getPrivateKey() -> String {  
       return ""  
    }  
}  

This class correctly uses the @objc and public attributes to expose your Swift code to Objective-C and therefore Kotlin.

Bridging Header Configuration

You created a bridging header that includes the Swift class:

#ifndef iosApp_Bridging_Header_h  
#define iosApp_Bridging_Header_h  
#import "iosApp-Swift.h"  
#endif /* iosApp_Bridging_Header_h */  

This header seems correct, as it correctly imports the generated Objective-C header that includes your Swift classes. However, ensure that this bridging header is indeed set in your project's build settings under "Objective-C Bridging Header".

If you're leveraging this header, the headers defined should also be visible to Kotlin. Validate that the path is accurate relative to your project structure.

Usage in Kotlin Multiplatform

You’ve defined your Kotlin expect class in the common module correctly as:

expect class WalletMP() {  
    fun getPrivateKey(): String  
}  

And your actual class in the iosMain module is defined as:

actual class WalletMP actual constructor() {  
    actual fun getPrivateKey(): String {  
        return ""  
    }  
}  

However, to call your Swift class, ensure that you instantiate Wallet correctly in your Kotlin:

val wallet = Wallet()  

Addressing Visibility Issues

If Wallet is still not accessible, consider the following steps:

  1. Check Framework Settings: Make sure that your Swift classes are included in the correct framework and that the framework is properly linked in your Kotlin project.
  2. Validate Build Settings: Look for any settings related to Swift version compatibility in both iOS and Kotlin Multi-platform. Sometimes, mismatched versions can lead to compatibility issues.
  3. Ensure Objective-C Interoperability: Verify that the Wallet class, along with methods you need, are marked with both @objc and public. This ensures Kotlin can indeed see and utilize it.
  4. Clean and Rebuild: After each configuration change, clean your build folder and rebuild your project to ensure all changes are correctly applied.

Frequently Asked Questions

Q: Why is my Swift class not visible to Kotlin?
A: This is usually due to incorrect bridging headers, visibility attribute misconfigurations, or improper linking of your framework.

Q: How can I debug visibility issues between Swift and Kotlin?
A: Check your bridging header configuration, ensure proper annotations are applied, and verify all settings in your project's build configurations.

Conclusion

Successfully integrating Swift with Kotlin in a Multiplatform project may involve several configuration details. By ensuring your class is correctly set up with public access, defining proper bridging headers, and following the steps outlined, you're likely to resolve visibility issues. Happy coding!