I would like to create a generic helper function that does some processing. One of its inputs will be a function returning an array of something.
I can't figure out how to do this. I keep getting a compile error. I found the post
argument for generic parameter could not be inferred
and tried adding ...as [String]
or ... as [Int]
, etc. but no luck.
func helperThatDoesSomeGenericProcessing<T>(displayedStrings: () -> [T]) -> [String]! {
let listOfSomething: [T] = displayedStrings()
// do something with listOfSomething
return ["some", "resulting", "string", "from", "the", "input"]
}
func concreteFunction1() -> [AnyObject]! {
var s: [String] = helperThatDoesSomeGenericProcessing { // ERROR: Argument for generic parameter 'T' could not be inferred.
var str = ["One", "Two", "Three"]
} // tried 'as [String]' here
// do something with s
return s
}
func concreteFunction2() -> [AnyObject]! {
var s: [Int] = helperThatDoesSomeGenericProcessing { // ERROR: Argument for generic parameter 'T' could not be inferred.
var i = [1, 2, 3]
} // tried 'as [Int]' here
// do something with s
return s
}