I want to define some default values in my shared ...
# multiplatform
s
I want to define some default values in my shared common, and use the values in Android and iOS but for some reason the default values are lost. somebody here that know whats up?
is there another way to do so?
n
What do you mean when you say they're not there? The generated Objective-C headers don't contain any implementation of your code - they're analogous to the
expect
declarations in Kotlin. You can access the values of
DimensionsObject
in Swift like this:
Copy code
let foo = DimensionsObject.shared.radiusSmall
s
In Android and iOS I would to share the same spacing values
those values are fixex and do not change
n
Sure, and you can. In this case you just have to use the
.shared
property to access the singleton instance of your
object
. You can read more about this and other things about Interoperability with Swift/Objective-C
s
I just get:
Type 'DimensionsObject' has no member 'shared'
n
Can you share a snippet of the Swift code you're using?
s
Copy code
import shared
    var body: some View {
        NavigationView() {
            ScrollView {
                LazyVGrid(columns: columns) {
                    ForEach(filteredPokemon, id: \.self) {pokemon in
                        PokemonOverviewItemCard(pokemon: pokemon)
                    }
                }
                .padding(.horizontal, CGFloat(DimensionsObject.shared.radiusSmall))
                .animation(.easeIn(duration: 0.3), value: filteredPokemon.count)
            }
            .refreshable { handleOnAppear() }
            .searchable(text: $searchText)
            .onAppear(perform: handleOnAppear)
            .navigationTitle("All Pokémon")
        }
        .navigationViewStyle(.stack)
    }
n
Okay. What if you change it to this:
Copy code
DimensionsObject().radiusSmall
This was how we used to access the properties of an
object
in previous versions of Kotlin/Native
s
that does the trick!
n
It's weird though. You should be able to use the
.shared
syntax 🤔
Which version of Kotlin are you using?
s
1.8.1
k
Copy code
object Constants {
val name = “iOS/Android” } we can access Constants().name in iOS Constants.name in android if you build without errors.