Lance Samaria
Lance Samaria

Reputation: 19572

Does Kotlin Have Something Similar to Swift's Guard-Let

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?

Upvotes: 0

Views: 36

Answers (0)

Related Questions