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

狂风中的少年 提交于 2019-12-02 02:28:49

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!