Bob*_*oin 9 generics nested-generics swift swift3.1
首先,感谢您的光临.我正在玩Swift 3.1嵌套泛型,我在初始化时遇到了错误.
class NestedProduct<T> {
enum Gadget {
case smartphone
case laptop
case fridge
case others(T)
}
enum Company {
case Samsung
case Apple
case Sony
case others(T)
}
let company: Company
let gadget: Gadget
let reviews: [T]
init(enterCompany: Company, enterGadget: Gadget, enterReView: [T]) {
company = enterCompany
gadget = enterGadget
reviews = enterReView
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我尝试初始化
let product = NestedProduct<String>(enterCompany: NestedProduct.Company.Apple,
enterGadget: NestedProduct.Gadget.laptop,
enterReView: ["Good"])
Run Code Online (Sandbox Code Playgroud)
但是,我收到一条错误消息,
GenericCache(0x11102a518):检测到循环元数据依赖性,中止
我不知道为什么会这样.你能帮忙吗?谢谢!
看起来这是一个已知问题:https://bugs.swift.org/browse/SR-3779
但是,我能够通过将枚举标记为间接来避免这种情况.这会将相关值存储在另一个破坏循环依赖关系的地方.
indirect enum Gadget {
case smartphone
case laptop
case fridge
case others(T)
}
indirect enum Company {
case Samsung
case Apple
case Sony
case others(T)
}
Run Code Online (Sandbox Code Playgroud)