1

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?

7
  • 1
    let intents = [messageIntents, weatherIntents, musicIntents] for an array of arrays Commented Jul 12, 2018 at 1:59
  • Then would that work for checking arrays? Commented Jul 12, 2018 at 2:00
  • Update your question to say what you mean by checking the arrays? Are you checking to see if a value is included? Commented Jul 12, 2018 at 2:01
  • Just updated it now, yes- to check for a value Commented Jul 12, 2018 at 2:03
  • let intents = messageIntents + weatherIntents + musicIntents for a single array with all elements, then if intents.contains("Send message") { print("found it!") } Commented Jul 12, 2018 at 2:10

4 Answers 4

1

You can simply add the array inside of array like this [[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 array in arraysOfArray{
    print(array)
   for dataObject in array{
    print(dataObject)
   }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

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
   }
}

Comments

0

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.

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.

Comments

0

You can use flatMap() to convert a 2D array into a 1D one, and check if that array contains the searched string:

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

Or, you can use contains(where:) over the 2D array, and contains() on the inner arrays:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.