Xcode 为什么我不能将我的类方法定义为包含变量的索引实例数组?
这就是我们所拥有的。这是Swift代码: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]. } } 在最后
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
}
}
谢谢你的回答!没问题,欢迎光临。请投票并勾选有助于解决问题的答案;-)谢谢你的回答!