ios 这里有什么问题:实例成员不能用于类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32693150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:45:40  来源:igfitidea点击:

What's wrong here: Instance member cannot be used on type

iosswiftswift2

提问by gurehbgui

I have the following code and I'm confused about this error message:

我有以下代码,但我对此错误消息感到困惑:

Instance member 'mydate' cannot be used on type 'TableViewController'

实例成员 'mydate' 不能用于类型 'TableViewController'

Code:

代码:

class TableViewController: UITableViewController {    
    let mydate = NSDate()
    let items = [
        (1, 9, 7, "A", mydate),
        (2, 9, 7, "B", mydate),
        (3, 9, 7, "C", mydate),
        (4, 9, 7, "D", mydate)
    ]

When I write the following, I can build it but I don't know why the oder snippet is not working:

当我编写以下内容时,我可以构建它,但我不知道为什么 oder 代码段不起作用:

class TableViewController: UITableViewController {    
    let mydate = NSDate()
    let items = [
        (1, 9, 7, "A", nil),
        (2, 9, 7, "B", mydate),
        (3, 9, 7, "C", mydate),
        (4, 9, 7, "D", mydate)
    ]

回答by avismara

The problem here is that you are using selfbefore the class is fully initialised. You can either have a getter which will be called every time you access the variable or compute it lazily.

这里的问题是您self在类完全初始化之前使用。您可以拥有一个每次访问变量时都会调用的 getter,也可以懒惰地计算它。

Here is some code:

这是一些代码:

class TableViewController: UITableViewController {
    let mydate = NSDate()
    var items : [(Int,Int,Int,String,NSDate)] {
        get {
            return [
                (1, 9, 7, "A", mydate),
                (2, 9, 7, "B", mydate),
                (3, 9, 7, "C", mydate),
                (4, 9, 7, "D", mydate)
            ]

        }
    }
}

Lazy computation:

懒惰计算:

class TableViewController: UITableViewController {
    let mydate = NSDate()
    lazy var items : [(Int,Int,Int,String,NSDate)] =  {

            return [
                (1, 9, 7, "A", self.mydate),
                (2, 9, 7, "B", self.mydate),
                (3, 9, 7, "C", self.mydate),
                (4, 9, 7, "D", self.mydate)
            ]


    }()
}

回答by r4id4

You can use this code

您可以使用此代码

var items:Array<(Int, Int, Int, String, NSDate)> {
        get {
            return [
                (1, 9, 7, "A", mydate),
                (2, 9, 7, "B", mydate),
                (3, 9, 7, "C", mydate),
                (4, 9, 7, "D", mydate)
            ]
        }
    }

回答by Adam

The compiler gets confused because it doesn't know the type of the optional NSDate. You can let it know explicitly about the type.

编译器会感到困惑,因为它不知道 optional 的类型NSDate。你可以让它明确知道类型。

let items : Array<(Int, Int, Int, String, NSDate?)> = [
    (1, 9, 7, "A", nil),
    (2, 9, 7, "B", mydate),
    (3, 9, 7, "C", mydate),
    (4, 9, 7, "D", mydate)
]

Edit: For the problem with using instance variable, you could initialise your items with a closure.

编辑:对于使用实例变量的问题,您可以使用闭包初始化您的项目。

let items : Array<(Int, Int, Int, String, NSDate?)> = {
    let mydate = NSDate()
    return [
        (1, 9, 7, "A", nil),
        (2, 9, 7, "B", mydate),
        (3, 9, 7, "C", mydate),
        (4, 9, 7, "D", mydate)
    ]
    }()