mercredi 2 mars 2016

any oneline code to achieve "if let" and then comparison?

I am doing code review on my colleague's Swift code and would like to refactor the following piece of code:

let status = JSON["status"]

if (status != nil && status as! String == "success") {
    ...
}

Can it be achieved using a single line of code?

I could only manage to make it like:

if let status = JSON["status"] as! String {
    if status == "success" {

I wish I could make it like:

if let status = JSON["status"] as! String == "success" {

But it does not compile... Can somebody help? Thanks. Is guard statement useful in this case?

I am actually a novice in Swift.

Aucun commentaire:

Enregistrer un commentaire