Hi all. I'm a little confused. According to <swift...
# ios
x
Hi all. I'm a little confused. According to swift-interopedia `Boolean` should be mapped to
Bool
, but on my end i'm seeing
KotlinBoolean
Copy code
__attribute__((swift_name("KotlinBoolean")))
@interface AppBoolean : AppNumber
- (instancetype)initWithBool:(BOOL)value;
+ (instancetype)numberWithBool:(BOOL)value;
@end
Does anyone know why? 🤔
c
In my experience, I see this: • If I have a Kotlin property or function that returns a
Boolean
and is consumed by Swift, then Swift sees it as a
Bool
• If I have a Kotlin property or function that I want Swift to implement, then the return type becomes
KotlinBoolean
Not sure if this is intended.
👀 1
So, let's say I have this:
Copy code
//Kotlin
data class MyUiState(
    val name: String,
    val loggedIn: Boolean
)
Then I can do this
Copy code
// Swift
if myUiState.loggedIn {
    doSomething()
}
However, if I have this interface
Copy code
// Kotlin
interface UserRepository {
    fun getName(): String
    fun isLoggedIn(): Boolean
}
Then I need to implement it like this in Swift. Notice how the return type of isLoggedIn became KotlinBoolean
Copy code
// Swift implementation
class IosUserRepository: UserRepository {
    func getName() -> String {
        return "John Doe"
    }

    func isLoggedIn() -> KotlinBoolean {
        let loggedIn = figureItOut()
        return KotlinBoolean(bool: loggedIn)
    }
}
Also, Swift usages of this function need to chain the
.boolValue
at the end:
Copy code
// Swift usage
let userRepository = IosUserRepository()
if userRepository.isLoggedIn().boolValue {
    doSomething()
}
x
yeah you are right. I'm wondering if this is documented somwhere. I also wonder why it works this way 🤔