arrays - 快速制作一个数组数组

标签 arrays swift

所以我的代码一直有问题,特别是在我有这些数组的地方:

var messageIntents = ["Send a message", "Send message", "Text", "Send text", "Send a text"]
var weatherIntents = ["weather", "what's the weather", "how's the weather", "the weather"]
var musicIntents = ["play", "i want to hear", "shuffle"]

目标是检查字符串是否等于数组中的值之一。我不想一个一个地检查这些数组,而是想制作一个数组来检查它们。那可能吗?我将如何检查它?

最佳答案

您可以像这样 [[Any]] 简单地将数组添加到数组内部。

    var messageIntents = ["Send a message", "Send message", "Text", "Send text", "Send a text"]
    var weatherIntents = ["weather", "what's the weather", "how's the weather", "the weather"]
    var musicIntents = ["play", "i want to hear", "shuffle"]

下面是数组占位符内的数组示例。

    var arraysOfArray = [messageIntents, weatherIntents,musicIntents]

您可以通过每次循环数组的索引来访问对象

arraysOfArray[index]
arraysOnArray[0] //prints ["Send a message", "Send message", "Text", "Send text", "Send a text"]

或者像这样使用'for each'语句

for array in arraysOfArray{
    print(array)
   for dataObject in array{
    print(dataObject)
   }
 }

关于arrays - 快速制作一个数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51296328/

相关文章:

c - 输出 20 更多数据

javascript - JS - 从数组中获取前 5 个最大元素

java - 在 Java 中,为什么 Array 的类构造函数是私有(private)的?

ios - 在 UITableViewCell 中使用 Timer 在 UILabel 中设置时间

ios - 无法识别的选择器发送到类 [NSCalendar calendarWithIdentifier]

ios - 如何使用 - swift - SpriteKit 编写将在特定时间运行函数的协议(protocol)

ios - 在 SwiftUI 中处理双重和三重手势识别器

Java - ArrayList - .size() 方法

swift - 基于QOS的DispatchSemaphore是否会阻塞整个线程池

arrays - Julia:数组是否包含特定的子数组