mapbox-maps-ios icon indicating copy to clipboard operation
mapbox-maps-ios copied to clipboard

Proposal: `Annotation.userInfo` should be non-optional

Open devioustree opened this issue 4 years ago • 0 comments

The current Annotation protocol defines userInfo as [String: Any]?

This can encourage some subtle bugs:

struct Polyline {
    var userInfo: [String: Any]?
}

// Assigning to nil
var foo = Polyline()
foo.userInfo?["test"] = 1

print(foo.userInfo ?? "Missing") // "Missing"

// Easiest solution
foo.userInfo = ["initial": "value"]
foo.userInfo = ["test": 1] // This will override any existing values

print(foo.userInfo ?? "Missing") // "["test": 1]" The initial value is missing

// More correct solution
foo.userInfo = foo.userInfo ?? [:]
foo.userInfo?["new"] = "value"

print(foo.userInfo ?? "Missing") // "["test": 1, "new": "value]"

Making userInfo non-optional would not entirely remove the possibility of bugs, but it would reduce the likelihood and make code much simpler.

devioustree avatar Dec 16 '21 19:12 devioustree