Continuing through a guard let block

So this is my code:

guard let currentAssignee = self.assigneeUserIdFor(groupId: groupID),
      let newAssignee = conversation.conversationAssignee,
      !newAssignee.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
      newAssignee != currentAssignee else {
    completion(response)
    return
}
print("looking for updating metadata")

The return statement is called when I run the function and the function exits. I want the function to go to the print statement and continue to update the metadata.
Because there is no loop, I cannot use a continue or a break.
How can I make this happen? Do I need to change the conditions in the guard statement?

The whole point of a guard statement is to exit the scope early. What would be the point of this statement if it executed the print statement regardless of whether the guard conditions were met?

I don't understand how this would achived the behavior you're looking for either.

1 Like

If the newAssignee is not the same as the current one, the call to the updateAssignee
function is done after the guard block. so, that is why I need the scope to continue even if the guard block fails..

So then why are you using a guard block at all if you want the print statement to execute regardless of whether the conditions are met? You should post the entire function for more context.

you can do

do {
  defer { print("...") }
  guard condition else {
    return
  }
}

Though this is quite stretching of the usefulness of guard (and defer). It may be better to use if-else.

2 Likes
            guard let currentAssignee = self.assigneeUserIdFor(groupId: groupID),
                  let newAssignee = conversation.conversationAssignee,
                  !newAssignee.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
                  newAssignee != currentAssignee else {
                completion(response)
                return
            }
            self.assignConversation(groupId: groupID, to: newAssignee) { result in
                switch result {
                case .success:
                    // if success, call updateMetadata`
                    completion(response)
                case .failure(let error):
                    let errorResponse = Response(success: false, clientChannelKey: clientId, error: error)
                    completion(errorResponse)
                }
            }

So, this is what I want to do.
If there is a new assignee, call assignConversation() and if that returns a success, call updateGroupMetadata()

The purpose of guard is to guarantee that everything in it succeeds before proceeding with the rest of a function — if any part of it fails, the else block gets executed, which includes leaving the function (or whatever scope you're in).

From your second message, it sounds like perhaps the newAssignee != currentAssignee condition doesn't actually require you to leave at that point, so you may want to extract that check from the guard statement and use it in an if or switch statement afterward.

1 Like
Terms of Service

Privacy Policy

Cookie Policy