Swift标准库将CommandLine
声明为枚举。
/// Command-line arguments for the current process.
public enum CommandLine {
/// Access to the raw argc value from C.
public static var argc: Int32 { get }
/// Access to the raw argv value from C. Accessing the argument vector
/// through this pointer is unsafe.
public static var unsafeArgv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?> { get }
/// Access to the swift arguments, also use lazy initialization of static
/// properties to safely initialize the swift arguments.
public static var arguments: [String]
}
通过将CommandLine
声明为结构可以很好地实现API的目的。
为什么将其声明为枚举而不是结构的任何特定原因?
答案 0 :(得分:5)
好吧,根据一些研究,我发现了这一点
使用不区分大小写的枚举的优势在于它不会被意外实例化,并且可以用作纯名称空间。(参考:https://github.com/raywenderlich/swift-style-guide#constants)
我相信这可能是原因之一。