Skip to content

Commit

Permalink
#747 get kotlin ObjC class but still invalid bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 committed Dec 7, 2024
1 parent 7ed538c commit 7a2bc6e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@
package dev.icerock.moko.resources.utils

import kotlinx.cinterop.BetaInteropApi
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.ExportObjCClass
import kotlinx.cinterop.ObjCClass
import kotlinx.cinterop.objcPtr
import platform.Foundation.NSBundle
import platform.Foundation.NSLog
import platform.darwin.NSObject

// can't be object, because https://github.com/JetBrains/kotlin-native/issues/3855#issuecomment-586990632
// in case of object we got error - java.lang.AssertionError: Assertion failed
@OptIn(BetaInteropApi::class)
@ExportObjCClass
internal object MRBundleAnchor : NSObject() {
class MRBundleAnchor : NSObject() {

@OptIn(ExperimentalForeignApi::class)
fun getBundle(): NSBundle {
val anchor: ObjCClass = requireNotNull(MRBundleAnchor.`class`()) {
"can't get class of $MRBundleAnchor"
// return NSBundle.mainBundle
NSLog("MOKO now i will search class anchor from $this")
val anchor: ObjCClass = requireNotNull(this.`class`()) {
"can't get class of $this"
}

NSLog("MOKO now i see anchor ${anchor.objcPtr()} and super is ${this.superclass().objcPtr()}")
return NSBundle.bundleForClass(anchor).also {
NSLog("MOKO i see bundle $it")
}
return NSBundle.bundleForClass(anchor)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun NSBundle.Companion.loadableBundle(identifier: String): NSBundle {
// external directory, not inside app directory (NSBundle.main). for example in case of
// SwiftUI preview - app directory empty, but dynamic framework with resources will be in
// different directory (DerivedData)
val bundlePath: String = MRBundleAnchor.getBundle().bundlePath
val bundlePath: String = MRBundleAnchor().getBundle().bundlePath

val enumerator: NSDirectoryEnumerator = requireNotNull(
NSFileManager.defaultManager.enumeratorAtPath(bundlePath)
Expand Down

0 comments on commit 7a2bc6e

Please sign in to comment.