'ViewController.Type'没有一个部件名为'阵列' [英] 'ViewController.Type' does not have a member named 'array'

查看:115
本文介绍了'ViewController.Type'没有一个部件名为'阵列'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到人们有这个问题不少。

I see people are having this problem a lot.

var array = [1,1]

class arrays {
var array2 = array //'ViewController.Type' does not have a member named 'array'
}

请帮忙

推荐答案

我假设你正在声明嵌套类,并使用阵列声明的ViewController 类阵列。 (我试图通过添加片段到的ViewController重现你的错误,正如你提到的:-)幸运地得到了同样的错误)

I assume you are declaring a Nested class and use a array declared in ViewController into a class arrays. (I tried reproducing your error by adding your snippet into a ViewController, and luckily got the same error as you mentioned :-) )

是的,它是可能的。看到下面的code。

Yes it is possible. see the following code.

var array = [1, 1]

class arrays {
    var array2 = ViewController().array
}

编辑:这是因为,在斯威夫特,一个内部类的一个实例是独立的外部类的任何实例。所以,你需要创建一个明确提及访问外部类的属性。

It is because, In Swift, an instance of an inner class is independent of any instance of the outer class. So you need to create an explicit reference to access the property of outer class.

希望这有助于。

这篇关于'ViewController.Type'没有一个部件名为'阵列'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆