Need help with translating this code to kotlin: ``...
# kotlin-native
m
Need help with translating this code to kotlin:
Copy code
- (void) MonitorWifiInterface
{
    m_pathMonitor = nw_path_monitor_create();
    
    nw_path_monitor_set_update_handler(m_pathMonitor, ^(nw_path_t  _Nonnull path) {
        NSLog(@"[NetInterfaceUtilies] Network path changed");
     });
 
     nw_path_monitor_start(m_pathMonitor); 
}
Have no idea what to do in
^(nw_path_t  _Nonnull path)
part
Here's what I have:
Copy code
actual class NetworkStatus {

    private val monitor = nw_path_monitor_create()
    private var path = nw_path_t()

    init {
        println("NETWORK: $path")
        nw_path_monitor_set_update_handler(monitor, path as nw_path_monitor_update_handler_t)
        nw_path_monitor_start(monitor)
    }

    actual fun isNetworkAvailable(): Boolean {
        return nw_path_get_status(path) == nw_path_status_satisfied
    }
}
but on cast I get error
kotlin.ClassCastException: null cannot be cast to kotlin.Function1
Found the solution (not working, but it compiles):
Copy code
actual class NetworkStatus {

    private val monitor = nw_path_monitor_create()
    private var path: nw_path_t = null

    init {
        nw_path_monitor_set_update_handler(monitor) { path ->
            this.path = path
        }
        nw_path_monitor_start(monitor)
    }

    actual fun isNetworkAvailable(): Boolean {
        return nw_path_get_status(path) == nw_path_status_satisfied
    }
}
👍 1