What's the best practice for naming Swift files that add extensions to existing objects?

后端 未结 7 664
面向向阳花
面向向阳花 2021-01-29 20:43

It\'s possible to add extensions to existing Swift object types using extensions, as described in the language specification.

As a result, it\'s possible to create exten

7条回答
  •  深忆病人
    2021-01-29 21:15

    Most examples I have seen mimic the Objective-C approach. The example extension above would be:

    String+UTF8Data.swift

    The advantages are that the naming convention makes it easy to understand that it is an extension, and which Class is being extended.

    The problem with using Extensions.swift or even StringExtensions.swift is that it's not possible to infer the purpose of the file by its name without looking at its contents.

    Using xxxable.swift approach as used by Java works okay for protocols or extensions that only define methods. But again, the example above defines an attribute so that UTF8Dataable.swift doesn't make much grammatical sense.

提交回复
热议问题