0

In Swift I can do the following which would 1- make sure that none of these properties are nil and 2- give me access to them safely unwrapped:

// user could be nil, and lat and lon are properties on the user object, they can also be nil. Auth.auth().currentUser?.uid is a firebase uid which could be nil too.
guard let user = post.user,
      let lat = user.lat,
      let lon = user.lon,
      let currentUID = Auth.auth().currentUser?.uid
else {
    return
}

print("latitude: \(lat), longtitude: \(lon), uid: \(currentUID)") 
// I now have access to lat, lon, and the current user's uid.

Does Kotlin offer any similar form of optional chaining where if anything is nil it'll trigger return but if none are I'm granted access to the unwrapped properties?

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • 1
    This looks to me like what you’re asking about. https://stackoverflow.com/questions/35513636/multiple-variable-let-in-kotlin – Tenfour04 May 30 '23 at 00:27
  • Exactly what I'm asking. I glanced over the answers, including the accepted answer, it seems you have to create a custom function to achieve what I'm looking for and it's not natively built into Kotlin. That's fine though, I'll pick one of them. THANKS for the response!!! :) – Lance Samaria May 30 '23 at 00:32
  • @LouisWasserman that's the same link Tenfour04 sent in the comments, thanks! I went with the **accepted answer** from that link. I left this question up here in case anyone from the Swift world comes along and reads this. They can refer to these comments for help. Cheers!!! – Lance Samaria May 30 '23 at 01:34

0 Answers0