continue after guard .. (fall down)

Hi,


I use guard to check nil for String

guard let myString = ro, !(ro?.isEmpty)! else {
                        self.lefttime = "0" 
                        continue
}

self.lefttime = myString


And I want to exexute self.lefttime = myString .....

but when myString is nil, then goto else phase then all stoped! (even if I use 'continue' end of else phase)


self.lefttime = myString is not executed ...


if I can not use myString below guard phase, then why 'guard' exist ...


Thanks😁

Accepted Reply

A guard statement cannot fall through, by design.


In general, use an "if" statement if you want control to fall through following the test. What's wrong with something like this:


if let myString = ro, !myString.isEmpty {
     self.lefttime = myString
}
else {
     self.lefttime = "0"
}


?

Replies

A guard statement cannot fall through, by design.


In general, use an "if" statement if you want control to fall through following the test. What's wrong with something like this:


if let myString = ro, !myString.isEmpty {
     self.lefttime = myString
}
else {
     self.lefttime = "0"
}


?

nothing wrong 😁