1
votes

I am getting the following error at the line of code below. My app crashes when the value is nil/empty, hence why I am trying to guard unwrap.

Error: Initializer for conditional binding must have Optional type, not '[String : String]'

guard let savedByUsers = venue.childSnapshot(forPath: "bookmarkedByUsers").value as! [String : String] else { return }
1
as! -> as? Conditionally unwrapping something you already force unwrapped is redundant. - John Montgomery
did you tried @JohnMontgomery suggestion ? - iOS Geek
@JohnMontgomery the suggestion is also included in the suggestion below. - Roggie

1 Answers

2
votes

change to:

guard let savedByUsers: [String: String] = venue.childSnapshot(forPath: "bookmarkedByUsers").value as? [String: String] else {
    return
}

The as! forces the unwrap and causes an exception when the value is nil. Using as? will avoid that and jump into the else clause.