I'm trying to implement a simple generic method:
func findMax<T: Comparable>(numbers_array : [T]) -> ( min_tuple : T , max_tuple : T )
{
var max_number : T?
var min_number : T?
for i in numbers_array
{
if(max_number < i)
{
max_number = i
}
if(min_number > i)
{
min_number = i
}
}
return (min_number! , max_number!)
}
I am trying to access the method like this:
let result = findMax([3.4, 5, -7, -7.8])
But whenever I run this code I get the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
I think thats because I haven't assigned a value to var max_number : T? and var min_number : T?
I can't assign a value to them because they are of generic type and I think the garbage value is messing with the logic of this function... I might be wrong but that is what I have been able to assess from my debugging session.
Thanks in advance, Any help is greatly appreciated.