Swift 从数组的数组及其数组号获取对象

Swift 从数组的数组及其数组号获取对象,swift,swift2,Swift,Swift2,我使用的是Swift 2.3,我的自定义对象的数组类型如下,称为Player 如何使用for in循环或其他方法来获取数组索引和对象 我有以下资料: for (index, p) in playing { -- Expression type [[Player]] is ambigious 我也试过了 for in (index, p: Player) in playing { -- same result. 及 我希望能够打印出对象所属的数组,然后使用当前对象使用枚举将索引和元素配对,如下

我使用的是Swift 2.3,我的自定义对象的数组类型如下,称为Player

如何使用for in循环或其他方法来获取数组索引和对象

我有以下资料:

for (index, p) in playing { -- Expression type [[Player]] is ambigious
我也试过了

for in (index, p: Player) in playing { -- same result.

我希望能够打印出对象所属的数组,然后使用当前对象

使用枚举将索引和元素配对,如下所示:

let a = [["hello", "world"], ["quick", "brown", "fox"]]
for outer in a.enumerated() {
    for inner in outer.element.enumerated() {
        print("array[\(outer.offset)][\(inner.offset)] = \(inner.element)")
    }
}
import Foundation

var playing = [["one", "two"], ["three", "four"]]

if let index = playing.index(where: { $0.contains("two") }) {
  print(index)
} else {
  print("Not found")
}
这将产生以下输出:

array[0][0] = hello
array[0][1] = world
array[1][0] = quick
array[1][1] = brown
array[1][2] = fox
使用enumerated将索引和元素配对,如下所示:

let a = [["hello", "world"], ["quick", "brown", "fox"]]
for outer in a.enumerated() {
    for inner in outer.element.enumerated() {
        print("array[\(outer.offset)][\(inner.offset)] = \(inner.element)")
    }
}
import Foundation

var playing = [["one", "two"], ["three", "four"]]

if let index = playing.index(where: { $0.contains("two") }) {
  print(index)
} else {
  print("Not found")
}
这将产生以下输出:

array[0][0] = hello
array[0][1] = world
array[1][0] = quick
array[1][1] = brown
array[1][2] = fox

我不会使用for循环,我会这样做:

let a = [["hello", "world"], ["quick", "brown", "fox"]]
for outer in a.enumerated() {
    for inner in outer.element.enumerated() {
        print("array[\(outer.offset)][\(inner.offset)] = \(inner.element)")
    }
}
import Foundation

var playing = [["one", "two"], ["three", "four"]]

if let index = playing.index(where: { $0.contains("two") }) {
  print(index)
} else {
  print("Not found")
}
这张照片是:

0

或者获取包含所需内容的整个子阵列:

if let subarray = playing.first(where: { $0.contains("three") }) {
  print(subarray)
} else {
  print("Not found")
}
印刷品:

[三,四]


我不会使用for循环,我会这样做:

let a = [["hello", "world"], ["quick", "brown", "fox"]]
for outer in a.enumerated() {
    for inner in outer.element.enumerated() {
        print("array[\(outer.offset)][\(inner.offset)] = \(inner.element)")
    }
}
import Foundation

var playing = [["one", "two"], ["three", "four"]]

if let index = playing.index(where: { $0.contains("two") }) {
  print(index)
} else {
  print("Not found")
}
这张照片是:

0

或者获取包含所需内容的整个子阵列:

if let subarray = playing.first(where: { $0.contains("three") }) {
  print(subarray)
} else {
  print("Not found")
}
印刷品:

[三,四]

功能方法:

let items = [["0, 0", "0, 1"], ["1, 0", "1, 1", "1, 2"]]
items.enumerated().forEach { (firstDimIndex, firstDimItem) in
    firstDimItem.enumerated().forEach({ (secondDimIndex, secondDimItem) in
        print("item: \(secondDimItem), is At Index: [\(firstDimIndex), \(secondDimIndex)]")
    })
}
印刷品:

项目:0,0位于索引:[0,0]

项目:0,1位于索引:[0,1]

项目:1,0位于索引:[1,0]

项目:1,1位于索引:[1,1]

项目:1,2位于索引:[1,2]

功能方法:

let items = [["0, 0", "0, 1"], ["1, 0", "1, 1", "1, 2"]]
items.enumerated().forEach { (firstDimIndex, firstDimItem) in
    firstDimItem.enumerated().forEach({ (secondDimIndex, secondDimItem) in
        print("item: \(secondDimItem), is At Index: [\(firstDimIndex), \(secondDimIndex)]")
    })
}
印刷品:

项目:0,0位于索引:[0,0]

项目:0,1位于索引:[0,1]

项目:1,0位于索引:[1,0]

项目:1,1位于索引:[1,1]

项目:1,2位于索引:[1,2]

谢谢,应在2.3中列举:-谢谢,应在2.3中列举:-