By adding return appropriately and explicitly declaring a specific type () -> [T] , we resolve the errors ... but I'm not sure if it will give you what you want. Anyway, here is the code:
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 { () -> [String] in // Explicit declared type var str = ["One", "Two", "Three"] return str } // tried 'as [String]' here // do something with s return s } func concreteFunction2() -> [AnyObject]! { var s: [String]! = helperThatDoesSomeGenericProcessing { () -> [Int] in // Explicit declared type var i = [1, 2, 3] return i } // tried 'as [Int]' here // do something with s return s }
Note that I also adjusted the var s type, since your common function always returns an implicitly expanded optional array of strings [String]! . The return type is not generalized (ie: T or [T] , etc.).
You may probably need to modify some type declarations to meet your design needs.
Hope this helps
source share