I currently have a function like this...
// this is a property
var currentString: String = ""
func doSomething() {
let newString: String = goGetANewString()
guard newString != currentString else {
return
}
currentString = newString
}
But I find it a bit odd that I am creating the newString
outside of the guard
.
If I move it into the guard
then it complains that it needs to be optional.
Is there a way of creating that newString
inside the guard
statement and checking the condition?
Ideally I'd like something like this, but like I said, it doesn't work this way.
func doSomething() {
guard let newString: String = goGetANewString(), newString != currentString else {
return
}
currentString = newString
}
goGetANewString
beforeguard
? – MoisesmoishenewString
if the guard succeeds. By creating thenewString
before the guard I then also have access to it inside theelse
of the guard. Which is fine... if I need it. But in this case I specifically don't. – Tache