Silver Challenge - No exact matches in call to subscript?

I get this error with my findAll(::slight_smile: function:

// silver challenge

func findAll<T: Equatable>(_ stuff: [T],_ searchTerm: T) -> [Int] {
    var items = [Int]()
    for i in 0..<stuff.count {
        if stuff[i].contains(searchTerm) { // No exact matches in call to subscript  error here
            items.append(i) 
        }
    }
    return items
}

I also get this error message in the console:

Swift.MutableCollection:3:23: note: candidate expects value of type '(UnboundedRange_) -> ()' for parameter #1
    @inlinable public subscript(x: (UnboundedRange_) -> ()) -> Self.SubSequence { get set }
                      ^

Anyone else see this?

The only thing the compiler knows about T is that it conforms to the Equatable protocol, so you have to work with what that gives you. The contains method is not part of that protocol, so you can’t use it on the array elements.

I don’t know what that console error means. Hopefully once you fix the other problem it will go away. :slight_smile:

1 Like

Ahh…

You know what it was? I looked at [T] as an array and thought I needed to use the contains() function. I used == instead:

// silver challenge

func findAll<T: Equatable>(_ stuff: [T],_ searchTerm: T) -> [Int] {
    var items = [Int]()
    for i in 0..<stuff.count {
        if stuff[i] == searchTerm {
            items.append(i)
        }
    }
    return items
}

// try findAll function
let cast = ["Tom", "Zendaya", "Benedict", "Marisa", "Jacob", "Tom"]
let fibbz = [0,1,1,2,3,5,8,13,21]

let membersTom = findAll(cast, "Tom") // [0,5]
let membersGladiator = findAll(cast, "Russell") // []

let weTheOnes = findAll(fibbz, 1) // [1,2]
let blackJack = findAll(fibbz, 21) // [8]