gpt4 book ai didi

swift - 您是否应该能够在不同的文件中声明两个具有相同名称的私有(private)变量?

转载 作者:搜寻专家 更新时间:2023-10-31 22:52:31 25 4
gpt4 key购买 nike

在 Swift 中,这样做会导致编译错误:

// file1.swift

private let testVar = 2

// file2.swift

private let testVar = 3

特别是,其中一个声明将以“无效的 testVar 重新声明”突出显示。


documentation一般而言,声明:

Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.

在讨论原始值时,它说:

The types used for any raw values or associated values in an enumeration definition must have an access level at least as high as the enumeration’s access level. You cannot use a private type as the raw value type of an enumeration with an internal access level, for example.

关于常量:

A constant, variable, or property cannot be more public than its type. It is not valid to write a public property with a private type, for example. Similarly, a subscript cannot be more public than either its index type or return type.


对我来说,这些摘录都没有暗示您不应该在单独的文件中拥有具有相同名称的 private 变量。

的确,第一句话对我说你应该能够;否则,您将泄露实现细节(即,您使用了一个名为 testVar 的变量)。


在提交错误报告之前,我想检查一下我没有误解语义。这应该是可能的吗?

最佳答案

这已在 Xcode 6.1 beta 2 中修复:

Issues Resolved in Xcode 6.1 Beta

Swift Language

Private entities with the same name and same type will no longer conflict if defined in different files within the same module. (17632175)

关于swift - 您是否应该能够在不同的文件中声明两个具有相同名称的私有(private)变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25527034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com