Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 为什么我不能将我的类方法定义为包含变量的索引实例数组?_Xcode_Swift - Fatal编程技术网

Xcode 为什么我不能将我的类方法定义为包含变量的索引实例数组?

Xcode 为什么我不能将我的类方法定义为包含变量的索引实例数组?,xcode,swift,Xcode,Swift,这就是我们所拥有的。这是Swift代码: class Process { // Some methods and properties } class SomeClass { var list: [Process]? = nil // ... func someFunc () { // ... if list !=nil { for i in list! { if list[i]. } } 在最后

这就是我们所拥有的。这是Swift代码:

class Process {
// Some methods and properties
}

class SomeClass {
    var list: [Process]? = nil
// ...
    func someFunc () {
    // ...
    if list !=nil {
            for i in list! {
                if list[i].
    }
}
在最后一行插入一个点(
),我没有得到方法和属性的列表

如果我将索引
I
更改为digit,那么我将获得其类进程的方法和属性列表

我应该怎么做才能在索引中使用变量?我可以使用它的类的方法

另外,对不起我的英语,我使用了机器翻译,如果建议的方法不起作用或没有意义,通常是因为你有语法错误。自动缩进代码也是如此

在这里,您所做的是迭代
列表
,并期望它为您提供一个索引,而它真正做的是为您提供
列表
中的对象

因此,代码应该是:

for i in list! {
    if i.XXX ...

其中,
XXX
是您试图测试的内容。

使用
for x in y
您不必下标,因为
x
不是索引,它是当前迭代器对象:

func someFunc () {
    if let list = list {
        for item in list {
            item.
        }       ^^^^ here auto-suggest will work
    }
}

谢谢你的回答!没问题,欢迎光临。请投票并勾选有助于解决问题的答案;-)谢谢你的回答!