在swift中循环遍历多维数组

时间:2018-02-07 04:29:10

标签: swift optional

所以我试图迭代NSArray。我的NSArray是一个字符串数组的数组。这是前1.5个元素的复制粘贴

a=with(dat,ave(Occupancy.,sub(".*?\\/","",Date),ID,FUN=function(x)mean(x,na.rm=T)))
> transform(dat,b=replace(x<-Occupancy.,y<-is.na(x),a[y]))
       Date ID Occupancy.        b
1  1/2/2018  1         95 95.00000
2  2/2/2018  1         94 94.00000
3  3/2/2018  1         94 94.00000
4  4/2/2018  1         96 96.00000
5  5/2/2018  1         94 94.00000
6  6/2/2018  1         NA 94.71429
7  7/2/2018  1         96 96.00000
8  8/2/2018  1         94 94.00000
9  1/2/2018  2         75 75.00000
10 2/2/2018  2         NA 78.33333
11 3/2/2018  2         79 79.00000
12 4/2/2018  2         82 82.00000
13 5/2/2018  2         NA 78.33333
14 6/2/2018  2         76 76.00000
15 7/2/2018  2         78 78.00000
16 8/2/2018  2         80 80.00000

这是让我头疼的功能

(
    (
    "Tater Tot Nachos",
    "Fried Feta",
    "The Ultimate Feta Bread",
    "Cheese Bread",
    "Aubrees Bread",
    "The Wings!",
    "Coconut Grove Chicken Sicks",
    "Far East Wings",
    "Bacon Brussels Sprouts"
),
    (
    "Shaved Brussels Sprout Salad",
    "Greek Salad",
    "Coronado Cobb Salad",
    "Harvest Salad",

&#39; cellDescripters&#39;是一个全局变量,它是我在顶部概述的数组,基本上是一个字符串数组的数组。

当我打印出类型的&#39; item&#39;我看到它的类型为__NSArrayM,这是我理解的NSMutableArray。查看文档NSMutableArrays是可迭代的。

然而,当我去编译这段代码时,我得到错误:

 func createMenu() {
    if let list = cellDescripters {
        for(index, item) in list.enumerated() {
            for food in item {
                //DO SOMETHING WITH "FOOD"
            }

        }
    }
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

我认为以下示例可以为您提供帮助 例如,我有像你一样的字符串数组数组

  

[[&#34;饮料&#34;,&#34;食品&#34;,&#34;供应商&#34;],[&#34;其他东西&#34;,&#34;药品&# 34;]];

img {
    width: 100%;
}

<强>输出

  var arrayExample = [["beverages", "food", "suppliers"],["other stuff", "medicine"]];
//Iterate array through for loop 
       for(index, item) in arrayExample.enumerated() 
         {
            for food in item 
             {
                print("index : \(index) item: \(food)")
            }
       }

答案 1 :(得分:0)

这是在Swift中迭代2D数组的更通用的解决方案。已在iOS 13的Swift 4中测试。 仅适用于Swift数组,请参见以下链接将NSArray转换为数组:https://stackoverflow.com/a/40646875/1960938

// 2D array extension explanation: https://stackoverflow.com/a/44201792/1960938
fileprivate extension Array where Element : Collection, Element.Index == Int {

    typealias InnerCollection = Element
    typealias InnerElement = InnerCollection.Iterator.Element

    func matrixIterator() -> AnyIterator<InnerElement> {
        var outerIndex = self.startIndex
        var innerIndex: Int?

        return AnyIterator({
            guard !self.isEmpty else { return nil }

            var innerArray = self[outerIndex]
            if !innerArray.isEmpty && innerIndex == nil {
                innerIndex = innerArray.startIndex
            }

            // This loop makes sure to skip empty internal arrays
            while innerArray.isEmpty || (innerIndex != nil && innerIndex! == innerArray.endIndex) {
                outerIndex = self.index(after: outerIndex)
                if outerIndex == self.endIndex { return nil }
                innerArray = self[outerIndex]
                innerIndex = innerArray.startIndex
            }

            let result = self[outerIndex][innerIndex!]
            innerIndex = innerArray.index(after: innerIndex!)

            return result
        })
    }

}

用法示例:

let sampleMatrix = [
    ["a", "b", "c"],
    ["d", "e"],
    [],
    ["f"],
    []
]

// Should print: a, b, c, d, e, f      
for element in sampleMatrix.matrixIterator() {
    print(element)
}