why swift use a struct as dictionary key instead of a string here?

前端 未结 1 663
轻奢々
轻奢々 2021-01-22 03:46

Why UIImagePickerController.InfoKey type is struct not a string ,what is the benefit to use struct as a dictionary key instead of string?

public str         


        
相关标签:
1条回答
  • 2021-01-22 04:01

    It's simple, the benefit is the ability to check key values during compilation. This way, you won't be able to pass a String directly and if you do, you have to manually wrap it into a InfoKey structure, making your intention clear.

    Most of the times you should be using one of the predefined constants.

    An enum would be a better solution but probably that would break some existing code (and cannot be really enforced in Objective-C).

    If Apple engineers were creating a new API today, they wouldn't probably even use a dictionary but they would use a custom object to pass the values to the delegate method.

    0 讨论(0)
提交回复
热议问题