Compiler complains that 'Expression resolved to unused function' when removing index in array of functions

3.5k Views Asked by At

I created this test case with 2 types and as expected the 'Int' case works. The Callback one doesn't. Not sure why this is happening. I've been trying lots of things. I may be missing something obvious?

typealias Type1 = Int
typealias Type2 = ([AnyObject?]) -> Void

class Test {
    private var cb1: [Type1]
    private var cb2: [Type2]

    init() {
        cb1 = [Type1](count: 2, repeatedValue: 0)
        cb2 = [Type2](count: 2, repeatedValue: { _ in })
    }

    func removeAtIndex(index: Int) {
        cb1.removeAtIndex(index)
        cb2.removeAtIndex(index)
    }

}
1

There are 1 best solutions below

2
On BEST ANSWER

Resolving to an unused function is trying to tell you that you have an expression that is returning a function type, but you never call it

So,you just need try to use it

typealias Type1 = Int
typealias Type2 = ([AnyObject?]) -> Void

class Test {
private var cb1: [Type1]
private var cb2: [Type2]

init() {
    cb1 = [Type1](count: 2, repeatedValue: 0)
    cb2 = [Type2](count: 2, repeatedValue: { _ in })
}

func removeAtIndex(index: Int) {
    cb1.removeAtIndex(index)
  let result = cb2.removeAtIndex(index)
}
}