所以我试图迭代一个 NSArray。我的 NSArray 是一个字符串数组的数组。这是前 1.5 个元素的复制粘贴
(
(
"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",
Run Code Online (Sandbox Code Playgroud)
这是让我头疼的功能
func createMenu() {
if let list = cellDescripters {
for(index, item) in list.enumerated() {
for food in item {
//DO SOMETHING WITH "FOOD"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
' cellDescripters ' 是一个全局变量,它是我在顶部概述的数组,基本上是一个字符串数组数组。
当我打印“ item ”的类型时,我看到它是 __NSArrayM 类型,根据我的理解,它是一个 NSMutableArray。查看文档 NSMutableArrays 是可迭代的。
但是,当我编译此代码时,出现错误:
Type 'Any' does not conform to protocol 'Sequence'
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
我认为以下示例可以为您提供帮助,例如我有像您这样的字符串数组数组 =
[[“饮料”,“食品”,“供应商”],[“其他东西”,“药品”]];
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)")
}
}
Run Code Online (Sandbox Code Playgroud)
输出
index : 0 item: beverages
index : 0 item: food
index : 0 item: suppliers
index : 1 item: other stuff
index : 1 item: medicine
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7575 次 |
| 最近记录: |