(Swift 3)generic parameter T could not be inferred

Viewed 8998

I am trying to create a function which takes in nested arrays of objects like [1,[2,[3,4],[5,6]]] and return values in single array like this [1,2,3,4,5,6]. I wanted to do it for generic objects so I created a method

func getNestedArray<T>(array:[Any])->[T]?{

    var nestedArray:[T] = []
    for object in array{

        if object is [Any]{
        let neededArray = getNestedArray(array: object as! [Any])
        nestedArray.append(contentsOf: neededArray)
        }
        else if object is T{
            nestedArray.append(object as! T)
        }
        else{
         print("send proper array dumbass")
            return nil
        }
    }
    return nestedArray
}

so if the object is of type T I just append it or if it is of type array I am calling that function again and recursively should give me the correct result but I am getting the error at while calling this function. what am i doing wrong? I am just passing 'Any' type object as '[Any]'. Why does it throw this error?

1 Answers
Related