为什么我不能实例化嵌套类的空数组? [英] Why can't I instantiate an empty array of a nested class?
问题描述
我似乎在使用 [foo]()
样式语法实例化嵌套类类型的空数组时遇到问题:
I seem to be having problems instantiating an empty array of a nested class type using the [foo]()
style syntax:
// Playground - noun: a place where people can play
class outsideClass {
}
class Wrapper {
class InsideClass {
}
}
var foo = [outsideClass]() // Works fine
// Invalid use of '()' to call a value of non-function type '[Wrapper.InsideClass.Type]'
var bar = [Wrapper.InsideClass]()
这是我的误解吗?这是在我喝咖啡之前,但我已经检查了发行说明,我认为您应该能够参考这样的嵌套类——还是 beta 7 中的错误?
Is this something I'm misunderstanding—it's before my coffee, but I've checked the release notes, and I think you should be able to refer to nested classes like this—or a bug in beta 7?
这作为一种解决方法工作正常:
This works fine as a workaround:
var foobar: [Wrapper.InsideClass] = []
推荐答案
这绝对看起来像是编译器中的一个错误,尤其是当您允许实例化一个嵌套类的空数组时美好的;它根本不适用于初始化语法.
This definitely looks like a bug in the compiler, especially as you're allowed to instantiate an empty array of a nested class just fine; it simply doesn't work with the initialiser syntax.
我会提出一个错误.同时,对于遇到此问题的任何人,您都可以通过使用带有空数组和指定变量类的赋值语法来解决该问题,而不是使用构造函数语法:
I'll raise a bug. In the meantime, for anyone experiencing the problem, you can work around it by using assignment syntax with an empty array and a specified class for the variable, rather than constructor syntax:
var foobar: [Wrapper.InsideClass] = []
这篇关于为什么我不能实例化嵌套类的空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!