【发布时间】:2017-01-05 14:59:47
【问题描述】:
我有以下基本枚举:
enum Voicity {}
在各种不同的文件上,我扩展了上述枚举以存储我的应用程序用作中心站的各种功能和信息。
例如,我使用此枚举以编程方式创建我的 UI 元素。
extension Voicity {
enum sectionObject {
case textField(ofType: Voicity.UIElement.textField)
case button(ofType: Voicity.UIElement.button)
case label(ofType: Voicity.UIElement.label)
case line()
case photoCircle
case stackView(_: UIStackView)
case view(_: UIView)
var item: UIView {
switch self {
case .textField(let type):
return createTextField(ofType: type)
case .label(let type):
return createLabel(ofType: type)
case .line():
return createLine()
case .photoCircle:
return createPhotoCircle()
case .stackView(let view):
return view
case .view(let view):
return view
case .button(let type):
return createButton(ofType: type)
}
}
}
}
为了可读性/可维护性,我通过再次扩展 sectionObject 枚举将上面返回的函数分成单独的文件。
extension Voicity.sectionObject {
func createLabel(ofType type: Voicity.UIElement.label) -> UILabel {
// implementation here
}
}
上面的工作正常但是当我尝试声明createButton()时:
extension Voicity.sectionObject {
func createButton(ofType type: Voicity.UIElement.button) -> UIButton {
// implementation here
}
}
我收到以下错误:
sectionObject is not a member of Voicity.
为什么我不能在一个文件中扩展我的枚举,而我可以在另一个文件中这样做?
更新
createButton() 和 createLabel() 方法都在组/文件夹 Model 内。
枚举声明在组/文件夹Store内。
所以,他们是分开的。但令我惊讶的是,createLabel() 工作时 createButton() 不能工作。
当我的枚举案例再次位于组/文件夹Store 下的单个文件和组/文件夹Model 下的方法时,这一切都可以正常工作(方法位于不同的文件中)。当枚举案例扩大时,我需要重构它们。
此外,我刚刚尝试在声明 createLabel() 的扩展名中移动 createButton()。这一切都出于某种原因。
之前,我删除了声明createButton()方法的文件并重新创建了它,一切都一样。因此,这不是一些奇怪的 Xcode 解析问题。
【问题讨论】:
-
也许是访问修饰符的问题。虽然它应该默认为内部,所以它应该可以在包范围内访问 o.0
-
与亚历山大的想法有关:这两个扩展名是在同一个文件中吗?如果不是,它们肯定在同一个模块中吗?
-
@Alexander 请查看更新...
-
@HAS 请看更新...
-
我刚刚在完全相同的位置创建了相同的文件 (CreateButton.swift),一切似乎都运行良好。从以前开始就完全奇怪了,创建一个新文件并没有解决任何问题。