0

I have a function with a parameter of type [String]. I can call the function and it executes successfully. However, I recently encountered an error when adding a new data source, and I'm trying to debug. I'm switching on the array parameter, using the same values I'm passing in to call the function. Swift throws an error:

Swift Expression pattern of type [String] cannot match values of type [String]

Here's the section of code I'm working with:

func calcRelevance(array: [String]) {
    /* block of code */

    if relevanceArr.count >= 1 {
        //do something
    } else {
        switch array {
            case self.someArray: 
                self.label.text = "No results returned from some data source"
            case self.someOtherArray: 
                self.label.text = "No results returned from some other data source"
            default: 
                self.label.text = "Your search yielded no results. Please refine your search by tapping back and using more relevant search terms"
        }
    }
}

As I typed this out I realized that I may not be able to use a switch block inside an if statement. I'm not sure if control flow allows for such, which seems odd to get a type error from it if that's the case.

Also, I've looked at Switching on UIButton title: Expression pattern of type 'String' cannot match values of type 'String?!' and it has to do with unwrapping optionals which I don't believe applies here.

Community
  • 1
  • 1
froggomad
  • 1,747
  • 2
  • 17
  • 40
  • I don't think you can use an array in a switch statement, as switches work with integer values. Try using an if statement to compare them instead. – brimstone May 21 '17 at 17:53
  • I'm switching on a parameter, I would think I'd be able to switch on anything of the same type of that parameter. I'd think Swift would be able to infer that I'm saying if this array is being used for this parameter do this For instance, in another function, I'm comparing string values in a switch statement. But alas, I'll try the tried and true method of the if/else beast and report back – froggomad May 21 '17 at 17:59
  • I switched to an else if statement and it builds, but the label output doesn't change – froggomad May 21 '17 at 18:08
  • Your relevanceArr probably has items in it. – brimstone May 21 '17 at 18:10
  • when relevanceArr isn't empty, the label populates with results from relevanceArr... the label is remaining at the initial value of "loading, please wait..." – froggomad May 21 '17 at 18:12
  • You can have switch statements inside switch statements inside if statements inside switch statements. The problem is that you can't use an array as the switch value. – xxtesaxx May 21 '17 at 18:12

1 Answers1

0

That's not how a switch is meant to work. The switch is not expecting case values to be arrays. If you want to compare arrays, use an if statement instead.

if array == someArray { 
    /* do something */ 
} else if array == someOtherArray { 
    /* do something else */ 
} else { 
    /* do another thing */ 
}
brimstone
  • 3,370
  • 3
  • 28
  • 49
  • I guess array is one of the few exceptions you cant use in switch statements – xxtesaxx May 21 '17 at 18:11
  • I'm not comparing arrays. I'm comparing the value of a function parameter where the expected type is an array – froggomad May 21 '17 at 18:13
  • So...you are comparing arrays? If the value of a parameter is an array, and you're comparing it to another array...am I missing something? – brimstone May 21 '17 at 18:15
  • In the switch statement What I'm saying is if this parameter holds this array, print this message to this label. Notice the function parameter (array: [String]) and the switch statement switching on array – froggomad May 21 '17 at 18:18
  • This is what my answer is doing though. It does something if the parameter is `someArray` or `someOtherArray`. It's what you were trying to do but the switch did not work. – brimstone May 21 '17 at 18:21
  • I tried this and it builds but doesn't execute (i.e. the label remains at the initial value) – froggomad May 21 '17 at 18:23
  • Is the function being called? Is the switch statement executing? (i.e. the relevanceArr is empty) – brimstone May 21 '17 at 18:25
  • I had an error in a block before these affecting the source array so this block wasn't executing though I thought it was. I need to brush up on async lol. Once I fixed that, the if/else block executes as expected. I'm surprised switch won't accept an array as a case. I remember learning that any hashable value could be switched, but I guess that would only include the array indices and not the array as a whole – froggomad May 21 '17 at 20:36