I am trying to extend the Array
type with a computed property called randElement, that's, obviously, returning a random element of the array, but I don't know how to deal with the fact that the type of elements that the Array
contains.
Here's what I'm trying to do:
extension Array {
var randElement:Array.Type {
var randNumber = Double(arc4random())/Double(UInt32.max)
randNumber*=(Double(self.count-1))
randNumber = floor(randNumber)
return self[Int(randNumber)]
}
}
It triggers me this error at the level of the return:
'T' is not convertible to 'Array<T>.Type
What am I doing wrong?