简体   繁体   English

快速制作一个数组

[英]make an array of arrays in swift

so I've been having trouble with my code, specifically where I have these arrays: 所以我一直在代码方面遇到麻烦,特别是在我拥有这些数组的地方:

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"]

The goal is to check to see if a string is equal to one of the values in the arrays. 目的是检查字符串是否等于数组中的值之一。 Rather than check these arrays one by one, I would like to make an array of arrays to check them all. 我不想一个接一个地检查这些数组,而是想制作一个数组数组来检查所有数组。 Is that possible? 那可能吗? And how would I check it? 而我将如何检查呢?

You can simply add the array inside of array like this [[Any]]. 您可以像[[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"]

Below an example of arrays inside of array placeholder. 下面是数组占位符内部的数组示例。

    var arraysOfArray = [messageIntents, weatherIntents,musicIntents]

You can access object by each looping the index of an array 您可以通过每次循环访问数组的索引来访问对象

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

or using 'for each' statement like this 或像这样使用“ for each”语句

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

You can store your array of strings in an array as well. 您也可以将字符串数组存储在数组中。 If your goal is to find a matching string within an array just use the contains function of Array. 如果您的目标是在数组中查找匹配的字符串,请使用Array的contains函数。

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"]

let arrayOfArrays = [messageIntents, weatherIntents, musicIntents ]

let stringToFind = "weather"
for array in arrayOfArrays {
   if array.contains(stringToFind) {
      print("Found It")
      break
   }
}

My understanding is that, you intend to categorize a string. 我的理解是,您打算对字符串进行分类。 If you intend to directly compare them ( by direct compare i mean no transformation during checking like hasPrefix , hasSuffix , contains , toLowerCase , toUpperCase ), then using a dictionary is the fastest way. 如果您打算直接比较它们(通过直接比较,我的意思是在检查期间不进行任何转换,例如hasPrefixhasSuffixcontainstoLowerCasetoUpperCase ),那么使用字典是最快的方法。

To do it in a cleaner way first of all declare an enum for categories 首先,以一种更简洁的方式声明类别的枚举

enum IntentCategories {
    case message
    case weather
    case music
    case none
}

Then storing the string keys in dictionary like following 然后将字符串键存储在字典中,如下所示

var intents [String: IntentCategories] = [
                                          "Send a message" :  IntentCategories.message,
                                          "Send message"   :  IntentCategories.message,
                                          "Text"           :  IntentCategories.message,
                                          "weather"        :  IntentCategories.weather
                 //... insert all the intents for message, hope you get my point                 
                ]

Then finding the string checking for particular intent category 然后找到检查特定意图类别的字符串

switch intents[INPUT_STRING] {
    case message:
         print("Message type intent detected")
    case weather:
         print("weather type intent detected")
    case music:
         print("music type intent detected")
    case none:
         print("undefined intent detected")
}

This implementation will give you the fastest possible way to categorize your intent strings. 此实现将为您提供最快的方式对意图字符串进行分类。

If you need transformation of existing intent before comparing, there is no quick way except linearly searching each intent strings. 如果在比较之前需要转换现有意图,则除了线性搜索每个意图字符串之外,没有其他快速方法。

Hope it helps. 希望能帮助到你。

You can use flatMap() to convert a 2D array into a 1D one, and check if that array contains the searched string: 您可以使用flatMap()将2D数组转换为1D数组,并检查该数组是否包含搜索到的字符串:

[messageIntents, weatherIntents, musicIntents].flatMap { $0 }.contains(searchedString)

Or, you can use contains(where:) over the 2D array, and contains() on the inner arrays: 或者,您可以在2D数组上使用contains(where:) ,并在内部数组上使用contains()

[messageIntents, weatherIntents, musicIntents].contains { $0.contains(searchedString) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM